C# 摄影机在旋转时跟随对象

C# 摄影机在旋转时跟随对象,c#,unity3d,camera,rotation,C#,Unity3d,Camera,Rotation,我正在尝试制作一个游戏,当用户移动时,相机会跟随用户。我将摄影机作为播放器的子对象,在编辑器中,摄影机围绕播放器旋转。当我玩游戏(在Unity中)并旋转玩家时,相机会直接旋转,而不是围绕玩家旋转,以保持相同的规则跟随距离。 顺便说一下,我使用transform.Rotate()来旋转播放器 总之: 寻找在3D游戏中跟随玩家的相机,当玩家转身时,它看起来没有什么不同 问题是,摄影机出现在编辑器中,可以完美地围绕播放器旋转,但在Unity中运行时调用transform.rotate()时就不会出现这

我正在尝试制作一个游戏,当用户移动时,相机会跟随用户。我将摄影机作为播放器的子对象,在编辑器中,摄影机围绕播放器旋转。当我玩游戏(在Unity中)并旋转玩家时,相机会直接旋转,而不是围绕玩家旋转,以保持相同的规则跟随距离。 顺便说一下,我使用transform.Rotate()来旋转播放器

总之:

  • 寻找在3D游戏中跟随玩家的相机,当玩家转身时,它看起来没有什么不同
  • 问题是,摄影机出现在编辑器中,可以完美地围绕播放器旋转,但在Unity中运行时调用transform.rotate()时就不会出现这种情况
  • 感谢您的帮助,欢迎您的帮助

    我把相机变成了播放器和编辑器的孩子

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

    您要做的是在
    Start()
    函数中获取相机和播放器之间的距离。这也称为偏移量。在
    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;
        }
    }
    
    我把相机变成了播放器和编辑器的孩子

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

    您要做的是在
    Start()
    函数中获取相机和播放器之间的距离。这也称为偏移量。在
    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;
        }
    }
    

    你的回答非常慷慨,而且很有帮助,谢谢you@Progammer我测试了你的回答,不幸的是它不起作用。@Ryan:如果答案对你不起作用,你为什么要接受它?相反,如果你接受它是因为你后来能够使它工作,也许你应该删除“它没有工作”的评论。你的回答很慷慨,很有帮助,谢谢you@Progammer我测试了你的回答,不幸的是它不起作用。@Ryan:如果答案对你不起作用,你为什么要接受它?相反,如果你接受它是因为你后来能够让它工作,也许你应该删除“它没有工作”的评论。