Unity fps rotation camera

前端 未结 3 1395
情深已故
情深已故 2021-02-05 15:45

In my game I have a camera and I want to have an FPS like rotation attached to this camera.

So if I move my cursor to the left, I want my cam to rotate to the left. If I

3条回答
  •  野性不改
    2021-02-05 16:27

    i created 2 scripts for my FPS project that work flawlessly for my movement (currently havnt added jump mechanics) and it seems like youve overcomplicated alot of things that can be quite simple and straight forward. im also new to coding so please correct me if im wrong heres my code

    // this is in the player script which i put on an empty parent object of the camera and player sprite

    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);
        }
    

    //this is the camera movement script which i put on the camera(will also use this script to add a function to change from 1st person to 3rd person for certain abilities)

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

提交回复
热议问题