【问题标题】:camera follows object when it rotates相机在旋转时跟随物体
【发布时间】:2016-09-28 22:54:00
【问题描述】:

我正在尝试制作一个游戏,在用户四处移动时,相机会跟随他们。我让相机成为玩家的孩子,在编辑器中,相机围绕玩家旋转很好。当我玩游戏(在 Unity 中)并旋转玩家时,相机实际上是在旋转而不是围绕玩家旋转,以保持相同的常规跟随距离。 顺便说一下,我使用 transform.Rotate() 来旋转播放器。

总结:

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

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

【问题讨论】:

    标签: c# unity3d camera rotation


    【解决方案1】:

    我让相机成为玩家和编辑器的孩子

    这一切都搞砸了。如果你想让它跟随玩家,你不要让相机成为一个孩子。

    您所做的是在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;
        }
    }
    

    【讨论】:

    • 您的回答非常慷慨,乐于助人,谢谢
    • @Progammer 我测试了你的回复,不幸的是它没有奏效。
    • @Ryan:如果答案对您不起作用,您为什么接受它?相反,如果你接受它是因为你后来能够让它工作,也许你应该删除说“它不起作用”的评论。
    猜你喜欢
    • 2012-10-11
    • 2022-01-11
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多