Unity fps rotation camera

前端 未结 3 1388
情深已故
情深已故 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:08

    Simple Script, Not Sure it will work with you, might as well give it a try

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

提交回复
热议问题