#X Axis problem

1 messages · Page 1 of 1 (latest)

deft vault
#
using System.Collections.Generic;
using UnityEngine;

{
    public class Look : MonoBehaviour
    {
        public static bool cursorLocked = true;

        public Transform player;
        public Transform cams;

        public float xSensitivity;
        public float ySensitivity;
        public float maxAngle;

        private Quaternion camCenter;

        void Start()
        {
            camCenter = cams.localRotation; //set rotation original for camera to camCenter
        }

        private void Update()
        {
            SetY();
            SetX();
            UpdateCursorLock();
        }

        void SetY()
        {
            float t_input = Input.GetAxis("Mouse Y") * ySensitivity;
            Quaternion t_adj = Quaternion.AngleAxis(t_input, -Vector3.right);
            Quaternion t_delta = cams.localRotation * t_adj;

            if (Quaternion.Angle(camCenter, t_delta) < maxAngle)
            {
                cams.localRotation = t_delta;
            }
        }

        void SetX()
        {
            float t_input = Input.GetAxis("Mouse X") * xSensitivity;
            Quaternion t_adj = Quaternion.AngleAxis(t_input, Vector3.up);
            Quaternion t_delta = cams.localRotation * t_adj;
            player.localRotation = t_delta;

        }

        void UpdateCursorLock()
        {
            if (cursorLocked)
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;

                if (Input.GetKeyDown(KeyCode.Escape))
                {
                    cursorLocked = false;
                }
            }
            else
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;

                if (Input.GetKeyDown(KeyCode.Escape))
                {
                    cursorLocked = false;
                }
            }
        }
    }
}```
#

ohh fixed it