Unity 根据前后帧位置自动旋转

写在Update里面

     var newPos = transform.position;
        if (lastPos != newPos) {
            targetRotation = Quaternion.LookRotation(newPos - lastPos);
        }

        if (Math.Abs(transform.rotation.eulerAngles.y - targetRotation.eulerAngles.y) > 0.01f) {
            transform.rotation = Quaternion.Slerp(transform.rotation,
                Quaternion.Euler(new Vector3(0, targetRotation.eulerAngles.y, 0)), rotateSpeed);
        }

        lastPos = newPos;

 

posted @ 2021-06-03 19:42  三页菌  阅读(112)  评论(0编辑  收藏  举报