Search Unity

Smooth Mouse Orbit

Discussion in 'Scripting' started by Cooper37, Jan 31, 2014.

  1. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Can anyone figure out how to add a smooth rotation to my mouse orbit script. I'd be most grateful! :)

    Code (csharp):
    1.  
    2. #pragma strict
    3. #pragma implicit
    4. #pragma downcast
    5.  
    6. var target : Transform;
    7. var layermask : LayerMask;
    8. var distance = 25.0;
    9. var damping : float = 6.0;
    10.  
    11. var xSpeed = 120.0;
    12. var ySpeed = 120.0;
    13.  
    14. var yMinLimit = -10;
    15. var yMaxLimit = 75;
    16.  
    17. private var x = 0.0;
    18. private var y = 0.0;
    19.  
    20. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    21.  
    22. function Start () {
    23.     var angles = transform.eulerAngles;
    24.     x = angles.y;
    25.     y = angles.x;
    26.  
    27.     // Make the rigid body not change rotation
    28.     if (rigidbody)
    29.         rigidbody.freezeRotation = true;
    30. }
    31.  
    32. function LateUpdate () {              
    33.  
    34.     if (target){
    35.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    36.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    37.        
    38.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    39.                
    40.         var rotation = Quaternion.Euler(y, x, 0);
    41.         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    42.  
    43.         transform.rotation = rotation;
    44.         transform.position = position;
    45.     }
    46.    
    47.     var hit : RaycastHit;
    48.     if(Physics.Linecast(target.position, transform.position,hit,layermask)){
    49.         var tempDistance = Vector3.Distance(target.position,hit.point);
    50.         position = rotation * Vector3(0.0, 0.0, -tempDistance) + target.position;
    51.         transform.position = position;
    52.     }
    53. }
    54.  
    55. static function ClampAngle (angle : float, min : float, max : float) {
    56.     if (angle < -360)
    57.         angle += 360;
    58.     if (angle > 360)
    59.         angle -= 360;
    60.     return Mathf.Clamp (angle, min, max);
    61. }
    62.  
     
  2. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    SOLVED: The mouse rotation 3rd person camera script with smooth rotation and collision. :D

    Code (csharp):
    1.  
    2. #pragma strict
    3. #pragma implicit
    4. #pragma downcast
    5.  
    6. var target : Transform;
    7. var layermask : LayerMask;
    8. var distance = 25.0;
    9. var damping : float = 6.0;
    10.  
    11. var xSpeed = 120.0;
    12. var ySpeed = 120.0;
    13.  
    14. var yMinLimit = -10;
    15. var yMaxLimit = 75;
    16.  
    17. private var x = 0.0;
    18. private var y = 0.0;
    19.  
    20. var smoothTime = 0.3;
    21.  
    22. private var xSmooth = 0.0;
    23. private var ySmooth = 0.0;
    24. private var xVelocity = 0.0;
    25. private var yVelocity = 0.0;
    26.  
    27. private var posSmooth = Vector3.zero;
    28. private var posVelocity = Vector3.zero;
    29.  
    30. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    31.  
    32. function Start () {
    33.     var angles = transform.eulerAngles;
    34.     x = angles.y;
    35.     y = angles.x;
    36.  
    37.     // Make the rigid body not change rotation
    38.     if (rigidbody)
    39.         rigidbody.freezeRotation = true;
    40. }
    41.  
    42. function LateUpdate () {              
    43.  
    44.     if (target){
    45.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    46.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    47.        
    48.         xSmooth = Mathf.SmoothDamp(xSmooth, x, xVelocity, smoothTime);
    49.         ySmooth = Mathf.SmoothDamp(ySmooth, y, yVelocity, smoothTime);
    50.  
    51.         ySmooth = ClampAngle(ySmooth, yMinLimit, yMaxLimit);
    52.  
    53.         var rotation = Quaternion.Euler(ySmooth, xSmooth, 0);
    54.  
    55.        // posSmooth = Vector3.SmoothDamp(posSmooth,target.position,posVelocity,smoothTime);
    56.  
    57.         posSmooth = target.position; // no follow smoothing
    58.  
    59.         transform.rotation = rotation;
    60.         transform.position = rotation * Vector3(0.0, 0.0, -distance) + posSmooth;
    61.     }
    62.    
    63.     var hit : RaycastHit;
    64.     if(Physics.Linecast(target.position, transform.position,hit,layermask)){
    65.         var tempDistance = Vector3.Distance(target.position,hit.point);
    66.         position = rotation * Vector3(0.0, 0.0, -tempDistance) + target.position;
    67.         transform.position = position;
    68.     }
    69. }
    70.  
    71. static function ClampAngle (angle : float, min : float, max : float) {
    72.     if (angle < -360)
    73.         angle += 360;
    74.     if (angle > 360)
    75.         angle -= 360;
    76.     return Mathf.Clamp (angle, min, max);
    77. }
    78.  
     
  3. Domo23000

    Domo23000

    Joined:
    Jan 27, 2014
    Posts:
    5
    add this to the script because if you move your mouse up or don oo much making the y go too high or low, it will tkae a while for it to come back to where it'll move.

    if(y > yMaxLimit)
    y = yMaxLimit;

    if(y < yMinLimit)
    y = yMinLimit;