问题现象
Avatar人物经过JumpStart
起跳到InAir
状态下落时,被碰撞器卡住,造成重力计算中IsGround
始终为false状态,人物以InAir
状态滞留在空中,如下所示:
Character Controller
角色控制器与物体的Collider
碰撞器如下所示:
解决方法
- 在
Character Controller
同物体组件中检测角色控制器与其它物体的碰撞,通过回调函数OnControllerColliderHit
,该回调会在角色控制器移动过程中接触到碰撞器时被调用。
private void OnControllerColliderHit(ControllerColliderHit hit)
{
//碰撞到的物体
LOG.Info("GameObject {0} Tag {1}", hit.collider.name, hit.collider.tag);
}
- 记录碰撞点的法线方向,即
ControllerColliderHit
中的normal
变量:
- 通过
Debug.DrawLine
可以观察碰撞点法线方向:
private void OnControllerColliderHit(ControllerColliderHit hit)
{
//碰撞到的物体
LOG.Info("GameObject {0} Tag {1}", hit.collider.name, hit.collider.tag);
//记录碰撞的法线方向
HitNormal = hit.normal;
//绘制碰撞点法线方向
Debug.DrawLine(hit.point, hit.normal, Color.cyan);
}
- 可以通过对比当前帧与上一帧的人物坐标Y值是否发生变化来检测是否滞留在了空中,检测到滞留后,去获取上述步骤中记录的
normal
法线方向,让Character Controller
沿该方向进行移动,以退出碰撞:
//InAir状态停留事件
protected override void OnInAirStateStay()
{
base.OnInAirStateStay();
/******************************************************************************
* 如果在InAir空中状态中 坐标Y高度不再发生变化
* 很有可能是Character Controller被某个物体碰撞器卡住了
* 此时去获取碰撞的法线方向 让角色控制器沿该方向移动 以便退出碰撞
/*****************************************************************************/
if (AvatarTran.position.y == posYCache)
{
LOG.Info("【InAir】Maybe character controller stay in collider.");
cc.Move(4f * Time.deltaTime * Vector3.ProjectOnPlane(AvatarTran.GetComponent<MasterAvatarEventResponser>().HitNormal, AvatarTran.up).normalized);
}
//缓存y坐标
posYCache = AvatarTran.position.y;
}
- 再次被碰撞器阻碍后,角色控制器会自动沿碰撞点法线方向进行移动退出碰撞: