Skip to content

Instantly share code, notes, and snippets.

@wipermail
Last active August 13, 2023 21:51
Show Gist options
  • Save wipermail/4f3891d5e63c3522ceb95306f4f296b8 to your computer and use it in GitHub Desktop.
Save wipermail/4f3891d5e63c3522ceb95306f4f296b8 to your computer and use it in GitHub Desktop.
Framework sensitivity mouse X,Y - Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
[RequireComponent(typeof(Rigidbody))]
[AddComponentMenu("Custom/Control Script/Mouse Look")]
public class MouseLook : MonoBehaviour
{
public enum RotationAxes
{
MouseXY = 0,
MouseX = 1,
MouseY = 2,
}
public float sensitivityX = 9.0f;
public float sensitivityY = 9.0f;
public float minY = -45.0f;
public float maxY = 45.0f;
private float _rotationX = 0;
public RotationAxes axes = RotationAxes.MouseXY;
// Start is called before the first frame update
void Start()
{
Rigidbody body = GetComponent<Rigidbody>();
if (body != null)
{
body.freezeRotation = true;
}
}
// Update is called once per frame
void Update()
{
if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else if (axes == RotationAxes.MouseY)
{
sensY();
transform.localEulerAngles = new Vector3(_rotationX, transform.localEulerAngles.y, 0);
}
else
{
sensY();
transform.localEulerAngles = new Vector3(_rotationX, sensX(), 0);
}
}
private void sensY()
{
_rotationX -= Input.GetAxis("Mouse Y") * sensitivityY;
_rotationX = Mathf.Clamp(_rotationX, minY, maxY);
}
private float sensX()
{
float delta = Input.GetAxis("Mouse X") * sensitivityX;
return transform.localEulerAngles.y + delta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment