Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /// MouseLook rotates the transform based on the mouse delta.
- /// Minimum and Maximum values can be used to constrain the possible rotation
- /// To make an FPS style character:
- /// - Create a capsule.
- /// - Add the MouseLook script to the capsule.
- /// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
- /// - Add FPSInputController script to the capsule
- /// -> A CharacterMotor and a CharacterController component will be automatically added.
- /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
- /// - Add a MouseLook script to the camera.
- /// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
- ///
- /// -- Added Smooth Look
- [AddComponentMenu("Camera-Control/Smooth Mouse Look")]
- public class SmoothMouseLook : MonoBehaviour
- {
- public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
- public RotationAxes axes = RotationAxes.MouseXAndY;
- public float Dumping = 0.4f;
- public float sensitivityX = 3F;
- public float sensitivityY = 3F;
- //public float minimumX = -360F;
- //public float maximumX = 360F;
- public float minimumY = -60F;
- public float maximumY = 60F;
- public Vector2 V;
- public static bool isLocked;
- float rotationY = 0F;
- float rotationX = 0F;
- GameObject tmpTrance;
- void Update()
- {
- if (isLocked) return;
- rotationY = 0f;
- rotationX = 0f;
- if (axes == RotationAxes.MouseXAndY)
- {
- // NOT WORKING )
- // CORRECT PLEASE )
- return;
- //float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
- //ART
- // rotationX = tmpTrance.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
- //
- // rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
- // rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
- // print("A");
- // //transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
- // tmpTrance.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
- // transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
- }
- else if (axes == RotationAxes.MouseX)
- {
- if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
- {
- //transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
- rotationX = (Input.GetAxis("Mouse X") + V.x) * sensitivityX;
- tmpTrance.transform.localEulerAngles = new Vector3(tmpTrance.transform.localEulerAngles.x, tmpTrance.transform.localEulerAngles.y + rotationX, tmpTrance.transform.localEulerAngles.z);
- }
- transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
- }
- else
- {
- if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
- {
- rotationY = tmpX - (V.y + Input.GetAxis("Mouse Y")) * sensitivityY;
- rotationY = ClampAngle(rotationY, minimumY, maximumY);
- tmpX = rotationY;
- tmpTrance.transform.localEulerAngles = new Vector3(rotationY, tmpTrance.transform.localEulerAngles.y, tmpTrance.transform.localEulerAngles.z);
- }
- //transform.localEulerAngles = Vector3.Lerp(transform.localEulerAngles, tmpTrance.transform.localEulerAngles, Dumping);
- transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
- }
- V = Vector2.zero;
- //tmpTrance.transform.localEulerAngles = new Vector3(-rotationY, tmpTrance.transform.localEulerAngles.y + rotationX, 0);
- //transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
- }
- public void Reset()
- {
- tmpTrance.transform.position = transform.position;
- tmpTrance.transform.rotation = transform.rotation;
- tmpX = tmpTrance.transform.localEulerAngles.x;
- }
- private float tmpX ;
- void Start()
- {
- if (!tmpTrance)
- {
- tmpTrance = new GameObject("SmoothTargetFor " + gameObject.name);
- tmpTrance.transform.position = transform.position;
- tmpTrance.transform.rotation = transform.rotation;
- if (transform.parent)
- {
- tmpTrance.transform.parent = transform.parent;
- }
- tmpX = tmpTrance.transform.localEulerAngles.x;
- }
- // Make the rigid body not change rotation
- if (rigidbody) rigidbody.freezeRotation = true;
- }
- float ClampAngle(float angle, float min, float max)
- {
- if (angle < -360f)
- {
- angle += 360f;
- }
- if (angle > 360f)
- {
- angle -= 360f;
- }
- return Mathf.Clamp(angle, min, max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment