TexeL

Untitled

Jan 18th, 2013
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /// MouseLook rotates the transform based on the mouse delta.
  5. /// Minimum and Maximum values can be used to constrain the possible rotation
  6.  
  7. /// To make an FPS style character:
  8. /// - Create a capsule.
  9. /// - Add the MouseLook script to the capsule.
  10. ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
  11. /// - Add FPSInputController script to the capsule
  12. ///   -> A CharacterMotor and a CharacterController component will be automatically added.
  13.  
  14. /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
  15. /// - Add a MouseLook script to the camera.
  16. ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
  17. ///
  18. /// -- Added Smooth Look
  19.  
  20. [AddComponentMenu("Camera-Control/Smooth Mouse Look")]
  21. public class SmoothMouseLook : MonoBehaviour
  22. {
  23.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
  24.     public RotationAxes axes = RotationAxes.MouseXAndY;
  25.  
  26.     public float Dumping = 0.4f;
  27.  
  28.     public float sensitivityX = 3F;
  29.     public float sensitivityY = 3F;
  30.  
  31.     //public float minimumX = -360F;
  32.     //public float maximumX = 360F;
  33.  
  34.     public float minimumY = -60F;
  35.     public float maximumY = 60F;
  36.  
  37.     public Vector2 V;
  38.    
  39.     public static bool isLocked;
  40.     float rotationY = 0F;
  41.     float rotationX = 0F;
  42.  
  43.     GameObject tmpTrance;
  44.  
  45.     void Update()
  46.     {
  47.         if (isLocked) return;
  48.        
  49.         rotationY = 0f;
  50.         rotationX = 0f;
  51.  
  52.         if (axes == RotationAxes.MouseXAndY)
  53.         {
  54.             // NOT WORKING )
  55.             // CORRECT PLEASE )
  56.             return;
  57.             //float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  58.  
  59.  
  60.             //ART
  61. //          rotationX = tmpTrance.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  62. //
  63. //          rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  64. //          rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  65. //          print("A");
  66. //          //transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  67. //          tmpTrance.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  68. //          transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
  69.         }
  70.         else if (axes == RotationAxes.MouseX)
  71.         {
  72.             if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
  73.             {
  74.                 //transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
  75.                 rotationX = (Input.GetAxis("Mouse X") + V.x) * sensitivityX;
  76.  
  77.                 tmpTrance.transform.localEulerAngles = new Vector3(tmpTrance.transform.localEulerAngles.x, tmpTrance.transform.localEulerAngles.y + rotationX, tmpTrance.transform.localEulerAngles.z);
  78.             }
  79.  
  80.             transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
  81.         }
  82.         else
  83.         {
  84.             if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
  85.             {
  86.                 rotationY = tmpX - (V.y + Input.GetAxis("Mouse Y")) * sensitivityY;
  87.                
  88.                 rotationY = ClampAngle(rotationY, minimumY, maximumY);
  89.                 tmpX = rotationY;
  90.  
  91.                 tmpTrance.transform.localEulerAngles = new Vector3(rotationY, tmpTrance.transform.localEulerAngles.y, tmpTrance.transform.localEulerAngles.z);
  92.  
  93.             }
  94.             //transform.localEulerAngles = Vector3.Lerp(transform.localEulerAngles, tmpTrance.transform.localEulerAngles, Dumping);
  95.             transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
  96.         }
  97.         V = Vector2.zero;
  98.         //tmpTrance.transform.localEulerAngles = new Vector3(-rotationY, tmpTrance.transform.localEulerAngles.y + rotationX, 0);
  99.         //transform.rotation = Quaternion.Lerp(transform.rotation, tmpTrance.transform.rotation, Dumping);
  100.     }
  101.  
  102.     public void Reset()
  103.     {
  104.         tmpTrance.transform.position = transform.position;
  105.         tmpTrance.transform.rotation = transform.rotation;
  106.         tmpX = tmpTrance.transform.localEulerAngles.x;
  107.     }
  108.  
  109.     private float tmpX ;
  110.     void Start()
  111.     {
  112.         if (!tmpTrance)
  113.         {
  114.             tmpTrance = new GameObject("SmoothTargetFor " + gameObject.name);
  115.             tmpTrance.transform.position = transform.position;
  116.             tmpTrance.transform.rotation = transform.rotation;
  117.  
  118.             if (transform.parent)
  119.             {
  120.                 tmpTrance.transform.parent = transform.parent;
  121.             }
  122.             tmpX = tmpTrance.transform.localEulerAngles.x;
  123.         }
  124.  
  125.         // Make the rigid body not change rotation
  126.         if (rigidbody) rigidbody.freezeRotation = true;
  127.     }
  128.  
  129.     float ClampAngle(float angle, float min, float max)
  130.     {
  131.         if (angle < -360f)
  132.         {
  133.             angle += 360f;
  134.         }
  135.  
  136.         if (angle > 360f)
  137.         {
  138.             angle -= 360f;
  139.         }
  140.  
  141.         return Mathf.Clamp(angle, min, max);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment