角色的转向
/// <summary>
/// turn to target
/// </summary>
private void LookAtTarget(Vector3 target)
{
if(target!=Vector3.zero) // 判断转向是否为0,非0才执行
{
Quaternion dir = Quaternion.LookRotation(target);
transform.rotation=Quaternion.Lerp(transform.rotation ,dir ,rotationSpeed)
}
}
角色的移动
/// <summary>
/// move
/// </summary>
public void Move(float x, float z)
{
if (x != 0||z!=0)
{
//1.转向 前往方向;
LookAtTarget(new Vector3(x,0,z)); // y axis delegate the upward ; so it's shoudl be zero;
//2 向目标运动:核心:调用内置组件提供的方法【角色控制器 运动的方法】;
Vector3 motion = new Vector3(transform.forward.x, -1, transform.forward.z);// y =-1 indicate the gravity.
chController.Move(motion * Time.deltaTime * moveSpeed);
//3.播放运动的动画
chAnim.PlayAnimation("run");
}
else
{
chAnim.PlayAnimation("idle");
}
}