相机旋转时跟随 object

camera follows object when it rotates

我正在尝试制作一款相机跟随用户移动的游戏。我将相机设为播放器的 child 并且在编辑器中,相机可以很好地围绕播放器旋转。当我玩游戏(在 Unity 中)并旋转播放器时,相机实际上是旋转而不是围绕播放器旋转以保持相同的常规跟随距离。 顺便说一句,我用transform.Rotate()来旋转播放器。

总结:

  1. 寻找在 3D 游戏中跟随玩家的相机,当玩家转动时看起来没有什么不同。
  2. 问题是摄像机出现在编辑器中以完美地围绕玩家旋转,但在 Unity 运行时调用 transform.Rotate() 时却不是。

感谢所有帮助,tyvm 寻求帮助。

I made the camera a child of the player and in the editor

这样做一切都失败了。如果你想让它跟随玩家,你不要让相机成为 child。

你所做的是在Start()函数中获取相机和玩家之间的距离。这也称为offset。在 LateUpdate() 函数中,不断将相机移动到玩家的位置,然后将该偏移量添加到相机的位置。就这么简单。

public class CameraMover: MonoBehaviour
{
    public Transform playerTransform;
    public Transform mainCameraTransform = null;
    private Vector3 cameraOffset = Vector3.zero;

    void Start()
    {

        mainCameraTransform = Camera.main.transform;

        //Get camera-player Transform Offset that will be used to move the camera 
        cameraOffset = mainCameraTransform.position - playerTransform.position;
    }

    void LateUpdate()
    {
        //Move the camera to the position of the playerTransform with the offset that was saved in the begining
        mainCameraTransform.position = playerTransform.position + cameraOffset;
    }
}