unity 2D汽车控制,关于图片的平滑旋转

以下实现的是一个2D垂直视角汽车的控制

private void MoveUpdate(){
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");
    if (v != 0 || h != 0)
    {
        rigibody.velocity = Direction=new Vector2(h,v);
        targetAngle =Vector2.Angle(Vector2.down,Direction);
        if (h < 0f)//targetAngle只会取得0-180,所以在左边取负
        {    

targetAngle =-targetAngle;   

  }
    }
    currentAngle=Mathf.LerpAngle(currentAngle,targetAngle,RotateSpeed* Time.deltaTime);
    RotationTransform.localRotation=Quaternion.AngleAxis(currentAngle,Vector3.forward);
}

这里的RotationTransform是这个脚本的子物体Rotation的Transfrom,
这样旋转与移动不会相互影响
车图片朝下是下方向,所以得到向下与向目标方向的角度,对角度进行插值,使用LerpAngle与Lerp的差别在于,当角度在-90到180进行插值时旋转不是绕一个大圈。
在这之前我使用
RotationTransform.localRotation=Quaternion.Slerp(RoationTransform.localRotation,targetRotation,speed*Time.deltaTime);去插值,结果是从上到下的移动直接旋转x轴而不是我要的z轴,可能是因为它是球形插值没法只在z轴旋转,用rigidbogy去锁定也没有效果,所以就直接插值它的角度然后把角度转回Quaternion

猜你喜欢

转载自blog.csdn.net/QQ734821120/article/details/79029029
今日推荐