文章目录
- 简介
- 变量说明
- 实现
- 动画准备
- 动画状态机
- State 状态
- None
- Stand To Cover
- Is Covering
- Cover To Stand
- 高度适配
- 高度检测
- 脚部IK
简介
本文介绍如何在Unity中实现一个Avatar角色的智能掩体系统,效果如图所示:
初版1.0.0代码已上传至SKFramework
框架Package Manager
中:
变量说明
Cover Layer Mask
:掩体物体的Layer层级Shortcut Key
:进入、退出掩体状态的快捷键Box Cast Size
:寻找掩体所用物理检测的Box大小Box Cast Num
:寻找掩体所用物理检测的Box数量(maxDistance = boxCastSize * boxCastNum)Stand 2 Cover Speed
:切换至掩体状态的移动速度Cover 2 Stand Speed
:退出掩体状态的移动速度Stand 2 Cover Time
:切换至掩体状态的时长(动画时长决定)Cover 2 Stand Time
:退出掩体状态的时长(动画时长决定)Sneak Speed
:掩体状态下移动的速度Direction Lerp Speed
:左右方向的插值速度Head Radius
:头部的半径 用于物理检测 未检测到碰撞时身体高度向下调整 并启用脚部IKHead Down Cast Count Limit
:头部下方物理检测的次数限制(每次下降一个半径的单位进行检测)Ground Layer Mask
:地面的Layer层级 用于脚部IK检测地面Body Position Lerp Speed
:身体高度插值的速度Foot Position Lerp Speed
:脚部IK插值的速度Raycast Distance
:脚部IK检测用的距离Raycast Origin Height
:脚部IK检测的高度
实现
动画准备
Mixamo
:动作文件全部是在Mixamo
网站上下载的:
Humanoid
:Animation Type设为Humanoid人形动画:
Animation
:调整相关设置:
Root Transform Rotation Offset:此处设为-180,目的为了调整朝向,使其与Stand2Cover、Cover2Stand等动画连贯。
动画状态机
Animator Parameters
:添加相关参数:Stand2Cover
:bool类型,用于进入、退出掩体状态;Cover Direction
:float类型,用于控制左右方向的混合树;Cover Sneak
:float类型,用于控制移动的混合树。
Sub-State Machine
:创建一个子状态机,用于处于Cover相关状态:
Cover子状态机中添加Stand2Cover
、Cover2Stand
动画状态及Cover Direction
混合树:
Cover Direction
混合树:包含Cover Left
、Cover Right
子混合树,两个子混合树又分别包含其对应方向的Idle
和Sneak
动画。Cover Direction
参数用于控制进入Cover Left还是Cover Right,Cover Sneak
参数用于控制Idle和Sneak之间的混合:
IK Pass
:启用对应层级的IK Pass通道,计算脚部IK所需:
State 状态
定义相关状态:
None
:未在任何状态;Stand2Cover
:正在切换至掩体状态(切换过程)IsCovering
:正处于掩体状态Cover2Stand
:正在退出掩体状态(切换过程)
public enum State
{
None, //未在任何状态
Stand2Cover, //正在切换至掩体状态
IsCovering, //正处于掩体状态
Cover2Stand, //正在退出掩体状态
}
//当前状态
private State state = State.None;
/// <summary>
/// 当前状态
/// </summary>
public State CurrentState
{
get
{
return state;
}
}
None
未处于任何状态时,向身体前方进行BoxCast
物理检测寻找掩体,当检测到掩体时,按下指定快捷键则进入Stand2Cover切换过程:
//未处于掩体状态
case State.None:
{
//Box检测的中心点
Vector3 boxCastCenter = transform.position + transform.up;
//最大检测距离
float maxDistance = boxCastSize.z * boxCastNum;
//向身体前方进行Box检测 寻找掩体
castResult = Physics.BoxCast(boxCastCenter, boxCastSize * .5f, transform.forward, out hit, transform.rotation, maxDistance, coverLayerMask);
//调试:法线方向
Debug.DrawLine(hit.point, hit.point + hit.normal, Color.magenta);
//检测到掩体
if (castResult)
{
//按下快捷键 进入掩体状态
if (Input.GetKeyDown(shortcutKey))
{
//正在切换至掩体状态
state = State.Stand2Cover;
//播放动画
animator.SetBool(AnimParam.Stand2Cover, true);
//禁用其他人物控制系统
GetComponent<AvatarController>().enabled = false;
//默认右方(动画Stand2Cover默认右方)
targetCoverDirection = 1f;
//启用脚部IK
enableFootIk = true;
bodyYOffset = 0.04f;
}
}
}
break;
Stand To Cover
切换至掩体状态的过程中,向RaycastHit
中的法线反方向移动,移动到掩体前方:
case State.Stand2Cover:
{
//计时
stand2CoverTimer += Time.deltaTime;
if (stand2CoverTimer < stand2CoverTime)
{
//向法线反方向移动 到掩体前
cc.Move(-hit.normal * Time.deltaTime * stand2CoverSpeed);
//朝向 面向法线方向
transform.forward = Vector3.Lerp(transform.forward, -hit.normal, Time.deltaTime * stand2CoverSpeed);
}
else
{
//重置计时器
stand2CoverTimer = 0f;
//切换完成 进入掩体状态
state = State.IsCovering;
bodyYOffset = 0.02f;
}
}
break;
Is Covering
在掩体状态时,获取用户Horizontal
水平方向上的输入,通过输入控制Avatar转向左侧或右侧并进行Sneak
移动:
//获取水平方向输入
float horizontal = Input.GetAxis("Horizontal");
//目标方向 输入为负取-1 为正取1
if (horizontal != 0f)
{
targetCoverDirection = horizontal < 0f ? -1f : 1f;
castResult = Physics.BoxCast(transform.position + transform.up, boxCastSize * .5f, transform.forward, out hit, Quaternion.identity, boxCastSize.z * boxCastNum, coverLayerMask);
Debug.DrawLine(hit.point, hit.point + hit.normal, Color.magenta);
cc.Move(-hit.normal * sneakSpeed * Time.deltaTime);
transform.forward = Vector3.Lerp(transform.forward, -hit.normal, Time.deltaTime * stand2CoverSpeed);
}
//方向插值运算
coverDirection = Mathf.Lerp(coverDirection, targetCoverDirection, Time.deltaTime * directionLerpSpeed);
//动画 方向
animator.SetFloat(AnimParam.CoverDirection, coverDirection);
//动画 掩体状态行走
animator.SetFloat(AnimParam.CoverSneak, Mathf.Abs(horizontal));
//通过输入控制移动
cc.Move(horizontal * sneakSpeed * Time.deltaTime * transform.right);
按下快捷键时,退出掩体状态:
animator.SetBool(AnimParam.Stand2Cover, false);
state = State.Cover2Stand;
Cover To Stand
退出掩体状态的过程中,向身体后方移动:
//计时
cover2StandTimer += Time.deltaTime;
cover2StandTimer = Mathf.Clamp(cover2StandTimer, 0f, cover2StandTime);
if (cover2StandTimer < cover2StandTime)
{
//后移
cc.Move(cover2StandSpeed * Time.deltaTime * -transform.forward);
}
else
{
//重置计时器
cover2StandTimer = 0f;
state = State.None;
//启用其他人物控制脚本
GetComponent<AvatarController>().enabled = true;
}
高度适配
如图所示,当掩体的高度降低时,角色会逐渐下蹲调整高度,实现该功能一方面需要在头部进行物理检测,另一方面需要启用脚部的IK。
高度检测
高度检测贯穿于Stand2Cover
和IsCovering
状态中,注意观察下图中红色球的变动,当SphereCast
球形检测在初始高度未检测到掩体时,会下降一个球半径的单位再次进行检测,如果在限制次数中都未检测到掩体,则退出掩体状态,如果检测到掩体,则获取碰撞点和初始高度的delta
差值,该差值就是身体要下降的高度:
//头部物理检测的初始点
headSphereCastOrigin = transform.position + Vector3.up * headOriginPosY + transform.right * targetCoverDirection * headRadius * 2f;
//向前方进行球形检测(掩体状态下前方就是后脑勺的方向)
headCastResult = Physics.SphereCast(headSphereCastOrigin, headRadius, transform.forward, out RaycastHit headHit, coverLayerMask);
int i = 0;
if (!headCastResult)
{
for (i = 0; i < headDownCastCountLimit; i++)
{
//每次下降一个半径的单位进行检测
headSphereCastOrigin -= Vector3.up * headRadius;
headCastResult = Physics.SphereCast(headSphereCastOrigin, headRadius, transform.forward, out headHit, coverLayerMask);
if (headCastResult) break;
}
}
if (headCastResult)
{
Debug.DrawLine(headSphereCastOrigin, headHit.point, Color.green);
float delta = headOriginPosY - headHit.point.y;
targetBodyPositionY = originBodyPositionY - delta - headRadius;
Debug.DrawLine(headSphereCastOrigin, headSphereCastOrigin - Vector3.up * (delta + i * headRadius), Color.red);
}
检测的位置受Cover Direction
方向影响,当处于Cover Left
时,会在头部左侧一定单位进行检测,相反,处于Cover Right
时,会在头部右侧一定单位进行检测:
获取到身体要下降的高度后,在OnAnimatorIK
函数中调整Animator
组件的bodyPosition
属性:
Vector3 bodyPosition = animator.bodyPosition;
bodyPosition.y = Mathf.Lerp(lastBodyPositionY, targetBodyPositionY, bodyPositionLerpSpeed);
animator.bodyPosition = bodyPosition;
lastBodyPositionY = animator.bodyPosition.y;
脚部IK
单纯的下调身体高度会导致脚穿模到地面以下,因此需要启用脚部IK,不断调整脚的位置,脚部IK在前面的文章中有介绍,这里不再详细说明,代码如下:
private void FixedUpdate()
{
//未启用FootIK or 动画组件为空
if (!enableFootIk || animator == null) return;
#region 计算左脚IK
//左脚坐标
leftFootPosition = animator.GetBoneTransform(HumanBodyBones.LeftFoot).position;
leftFootPosition.y = transform.position.y + raycastOriginHeight;
//左脚 射线检测
leftFootRaycast = Physics.Raycast(leftFootPosition, Vector3.down, out RaycastHit hit, raycastDistance + raycastOriginHeight, groundLayerMask);
if (leftFootRaycast)
{
leftFootIkPosition = leftFootPosition;
leftFootIkPosition.y = hit.point.y + bodyYOffset;
leftFootIkRotation = Quaternion.FromToRotation(transform.up, hit.normal);
#if UNITY_EDITOR
//射线
Debug.DrawLine(leftFootPosition, leftFootPosition + Vector3.down * (raycastDistance + raycastOriginHeight), Color.yellow);
//法线
Debug.DrawLine(hit.point, hit.point + hit.normal * .5f, Color.cyan);
#endif
}
else
{
leftFootIkPosition = Vector3.zero;
}
#endregion
#region 计算右脚IK
//右脚坐标
rightFootPosition = animator.GetBoneTransform(HumanBodyBones.RightFoot).position;
rightFootPosition.y = transform.position.y + raycastOriginHeight;
//右脚 射线检测
rightFootRaycast = Physics.Raycast(rightFootPosition, Vector3.down, out hit, raycastDistance + raycastOriginHeight, groundLayerMask);
if (rightFootRaycast)
{
rightFootIkPosition = rightFootPosition;
rightFootIkPosition.y = hit.point.y + bodyYOffset;
rightFootIkRotation = Quaternion.FromToRotation(transform.up, hit.normal);
#if UNITY_EDITOR
//射线
Debug.DrawLine(rightFootPosition, rightFootPosition + Vector3.down * (raycastDistance + raycastOriginHeight), Color.yellow);
//法线
Debug.DrawLine(hit.point, hit.point + hit.normal * .5f, Color.cyan);
#endif
}
else
{
rightFootIkPosition = Vector3.zero;
}
#endregion
}
#region 应用左脚IK
//权重
animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot, 1f);
animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot, 1f);
Vector3 targetIkPosition = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
if (leftFootRaycast)
{
//转局部坐标
targetIkPosition = transform.InverseTransformPoint(targetIkPosition);
Vector3 world2Local = transform.InverseTransformPoint(leftFootIkPosition);
//插值计算
float y = Mathf.Lerp(lastLeftFootPositionY, world2Local.y, footPositionLerpSpeed);
targetIkPosition.y += y;
lastLeftFootPositionY = y;
//转全局坐标
targetIkPosition = transform.TransformPoint(targetIkPosition);
//当前旋转
Quaternion currRotation = animator.GetIKRotation(AvatarIKGoal.LeftFoot);
//目标旋转
Quaternion nextRotation = leftFootIkRotation * currRotation;
animator.SetIKRotation(AvatarIKGoal.LeftFoot, nextRotation);
}
animator.SetIKPosition(AvatarIKGoal.LeftFoot, targetIkPosition);
#endregion
#region 应用右脚IK
//权重
animator.SetIKPositionWeight(AvatarIKGoal.RightFoot, 1f);
animator.SetIKRotationWeight(AvatarIKGoal.RightFoot, 1f);
targetIkPosition = animator.GetIKPosition(AvatarIKGoal.RightFoot);
if (rightFootRaycast)
{
//转局部坐标
targetIkPosition = transform.InverseTransformPoint(targetIkPosition);
Vector3 world2Local = transform.InverseTransformPoint(rightFootIkPosition);
//插值计算
float y = Mathf.Lerp(lastRightFootPositionY, world2Local.y, footPositionLerpSpeed);
targetIkPosition.y += y;
lastRightFootPositionY = y;
//转全局坐标
targetIkPosition = transform.TransformPoint(targetIkPosition);
//当前旋转
Quaternion currRotation = animator.GetIKRotation(AvatarIKGoal.RightFoot);
//目标旋转
Quaternion nextRotation = rightFootIkRotation * currRotation;
animator.SetIKRotation(AvatarIKGoal.RightFoot, nextRotation);
}
animator.SetIKPosition(AvatarIKGoal.RightFoot, targetIkPosition);
#endregion