Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// One instance will turn the whole player body and one - only camera
- /// </summary>
- public enum RotationAxes { X, Y, Both };
- /// <summary>
- /// FPS mouse look.
- /// </summary>
- [AddComponentMenu("FPS Stuff/Smooth Mouse Look")]
- public class MouseLook : MonoBehaviour
- {
- public RotationAxes rotationAxes;
- public Vector2 mouseSensitivity;
- public Vector2 mouseSmoothness;
- public Vector2 verticalRestriction;
- private Vector2 lookDelta = Vector2.zero;
- private Vector2 smooth = Vector2.zero;
- private Vector2 smoothVelocity = Vector2.zero;
- void Update()
- {
- Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X") * mouseSensitivity.x,
- -Input.GetAxisRaw("Mouse Y") * mouseSensitivity.y);
- lookDelta += mouseDelta;
- // turn player on Y axis
- if (rotationAxes == RotationAxes.X)
- {
- smooth.x = Mathf.SmoothDamp(smooth.x, lookDelta.x, ref smoothVelocity.x, mouseSmoothness.x);
- Quaternion rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, smooth.x, transform.rotation.eulerAngles.z);
- rigidbody.MoveRotation(rotation);
- }
- else if (rotationAxes == RotationAxes.Y)
- {
- smooth.y = Mathf.SmoothDamp(smooth.y, lookDelta.y, ref smoothVelocity.y, mouseSmoothness.y);
- smooth.y = ClampAngle(smooth.y, verticalRestriction.x, verticalRestriction.y);
- Quaternion rotation = Quaternion.Euler(smooth.y, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
- transform.rotation = rotation; // camera has no rigidbody so use transform
- }
- }
- static float ClampAngle(float angle, float min, float max)
- {
- if (angle < -360)
- angle += 360;
- if (angle > 360)
- angle -= 360;
- return Mathf.Clamp(angle, min, max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement