【问题标题】:Unity fps rotation cameraunity fps 旋转相机
【发布时间】:2012-01-17 21:59:15
【问题描述】:

在我的游戏中,我有一个摄像头,我想在这个摄像头上附加一个类似旋转的 FPS。

因此,如果我将光标向左移动,我希望我的凸轮向左旋转。如果我向上移动光标,那么凸轮应该向上看,等等。

我目前有部分工作。我可以左右看,上看,下看。当我向下看然后左右移动光标时会出现问题。然后它给了我一个“滚动”的效果。

观看此视频以了解我的意思: http://www.screencast.com/t/Phedh8H0K13

很明显,当我往下看时,我仍然想要一个“Yaw”效果而不是“Roll”效果。有人知道怎么做吗?这是我目前所拥有的:

// Update is called once per frame
public override void update ()
{
    this.camera.transform.rotation *= 
        Quaternion.AngleAxis( Time.deltaTime * sensitivityRoll * Input.GetAxis("Vertical"), Vector3.forward );

    this.camera.transform.rotation *= 
        Quaternion.AngleAxis( Time.deltaTime * sensitivityYaw * Input.GetAxis("Mouse X"), Vector3.up );

    this.camera.transform.rotation *= 
        Quaternion.AngleAxis( Time.deltaTime * sensitivityPitch * Input.GetAxis("Mouse Y"), Vector3.left );
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    using UnityEngine;
    
    public class CameraRotator : MonoBehaviour
    {
        [SerializeField] private Transform _head;
        [SerializeField] private float _sensitivity;
        [SerializeField] private bool _invertVertical;
    
        private void Update()
        {
            var deltaMouse = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y");
            
            Vector2 deltaRotation = deltaMouse * _sensitivity;
            deltaRotation.y *= _invertVertical ? 1.0f : -1.0f;
    
            float pitchAngle = _head.localEulerAngles.x;
            
            // turns 270 deg into -90, etc
            if (pitchAngle > 180)
                pitchAngle -= 360;
    
            pitchAngle = Mathf.Clamp(pitchAngle + deltaRotation.y, -90.0f, 90.0f);
    
            transform.Rotate(Vector3.up, deltaRotation.x);
            _head.localRotation = Quaternion.Euler(pitchAngle, 0.0f, 0.0f);
        }
    }
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    【解决方案2】:

    我为我的 FPS 项目创建了 2 个脚本,它们完美地适用于我的动作(目前还没有添加跳跃机制),看起来你把很多本来可以非常简单直接的事情复杂化了。 我也是编码新手,所以如果我错了,请纠正我这是我的代码

    // 这是在播放器脚本中,我将它放在相机和播放器精灵的空父对象上

    public class player : MonoBehaviour
    {
        [Header("player Movement")]
        [SerializeField] float moveSpeed = 5f;
        [SerializeField] public float mouseSensitivity = 5f;
    
        
        Rigidbody rigidBody;
       
        // Use this for initialization
        void Start()
        {
            
        }
    
     void Update()
        {
            Move();
           
        }
    
    
        private void Move()
        {
            transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime);
            transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime);
            transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * mouseSensitivity);
        }
    

    //这是我放在相机上的相机移动脚本(也将使用这个脚本添加一个功能,从第一人称更改为第三人为某些能力)

    {
    
        player player;
        // Use this for initialization
        void Start()
        {
            player = FindObjectOfType<player>();
        }
    
        // Update is called once per frame
        void Update()
        {
            MoveView();
        }
        private void MoveView()
        {
            transform.Rotate(Vector3.left * Input.GetAxis("Mouse Y") * player.mouseSensitivity);
        }
    }
    

    【讨论】:

    • GetAxis 在鼠标轴上返回自上一帧以来鼠标绝对移动的差异。 不要--我重复,不要--将鼠标轴的 GetAxis 乘以 deltaTime除非您想要速度鼠标移动比鼠标移动的距离更重要。在大多数情况下,这真的很不直观,尤其是这个!
    • 而且,即使你这样做了,你也可能意味着在这些情况下除以deltaTime(因为速度 = 增量位置/增量时间)
    【解决方案3】:

    简单的脚本,不确定它是否适用于您,不妨尝试一下

    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Security.Cryptography;
    using System.Threading;
    using UnityEngine;
    
    public class mouse_lookat : MonoBehaviour
    {
        public float mouseSensitivity = 100f;
    
        public Transform playerBody;
    
        float xRotation = 0f;
    
        // Start is called before the first frame update
        void Start()
        {
            Cursor.lockState = CursorLockMode.Locked;
        }
    
        // Update is called once per frame
        void Update()
        {
            float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
            float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    
            xRotation -= mouseY;
            xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    
            transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
            playerBody.Rotate(Vector3.up * mouseX);
        }
    }
    

    【讨论】:

    • 为什么using System.Security.Cryptography;??
    【解决方案4】:

    我刚刚在这个话题中找到了我的答案:

    http://forum.unity3d.com/threads/109250-Looking-with-the-Mouse?highlight=person+camera


    该主题的代码:

    C# 单声道代码:

    using UnityEngine;
    using System.Collections;
    
    /// MouseLook rotates the transform based on the mouse delta.
    /// Minimum and Maximum values can be used to constrain the possible rotation
    
    /// To make an FPS style character:
    /// - Create a capsule.
    /// - Add the MouseLook script to the capsule.
    ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
    /// - Add FPSInputController script to the capsule
    ///   -> A CharacterMotor and a CharacterController component will be automatically added.
    
    /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
    /// - Add a MouseLook script to the camera.
    ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
    [AddComponentMenu("Camera-Control/Mouse Look")]
    public class MouseLook : MonoBehaviour {
    
        public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
        public RotationAxes axes = RotationAxes.MouseXAndY;
        public float sensitivityX = 15F;
        public float sensitivityY = 15F;
    
        public float minimumX = -360F;
        public float maximumX = 360F;
    
        public float minimumY = -60F;
        public float maximumY = 60F;
    
        float rotationY = 0F;
    
        void Update ()
        {
            if (axes == RotationAxes.MouseXAndY)
            {
                float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    
                transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
            }
            else if (axes == RotationAxes.MouseX)
            {
                transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
            }
            else
            {
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    
                transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
            }
        }
    
        void Start ()
        {
        //if(!networkView.isMine)
            //enabled = false;
    
            // Make the rigid body not change rotation
            //if (rigidbody)
                //rigidbody.freezeRotation = true;
        }
    }
    

    【讨论】:

    • 晚了几年,但感谢您的回答,这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    相关资源
    最近更新 更多