Search Unity

Dead Space Style Camera Help!

Discussion in 'Scripting' started by thommoboy, Feb 21, 2013.

  1. thommoboy

    thommoboy

    Joined:
    Jul 20, 2012
    Posts:
    59
    hey guys ive created a dead space style orbit camera but i want it to always stay on the right when the player rotates the camera infront of the player and moves in the direction the player has chose.

    Here is an example of what i have now and what i want:



    and here is the script:
    Code (csharp):
    1.  
    2. var target : Transform;
    3. var targetOffset = Vector3.zero;
    4. var distance = 4.0;
    5.  
    6. var lineOfSightMask : LayerMask = 0;
    7. var closerRadius : float = 0.2;
    8. var closerSnapLag : float = 0.2;
    9.  
    10. var xSpeed = 200.0;
    11. var ySpeed = 80.0;
    12.  
    13. var yMinLimit = -20;
    14. var yMaxLimit = 80;
    15.  
    16. private var currentDistance = 10.0;
    17. private var x = 0.0;
    18. private var y = 0.0;
    19. private var distanceVelocity = 0.0;
    20.  
    21. function Start () {
    22.     var angles = transform.eulerAngles;
    23.     x = angles.y;
    24.     y = angles.x;
    25.     currentDistance = distance;
    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.     {
    36.         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    37.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    38.        
    39.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    40.                
    41.         var rotation = Quaternion.Euler(y, x, 0);
    42.         var targetPos = target.position + targetOffset;
    43.         var direction = rotation * -Vector3.forward;
    44.        
    45.         var targetDistance = AdjustLineOfSight(targetPos, direction);
    46.         currentDistance = Mathf.SmoothDamp(currentDistance, targetDistance , distanceVelocity, closerSnapLag * .3);
    47.         transform.rotation = rotation;
    48.         transform.position = targetPos + direction * currentDistance;
    49.         /*
    50.         if(transform.localPosition.z > 0.8)
    51.         {
    52.             targetOffset.x = -0.55;
    53.             Debug.Log("Switch Camera Position");
    54.         }
    55.        
    56.         if(transform.localPosition.z < 0.8)
    57.         {
    58.             targetOffset.x = 0.55;
    59.             Debug.Log("Original Camera Position");
    60.         }
    61.         */
    62.     }
    63. }
    64.  
    65. function AdjustLineOfSight (target : Vector3, direction : Vector3) : float
    66. {
    67.     var hit : RaycastHit;
    68.     if (Physics.Raycast (target, direction, hit, distance, lineOfSightMask.value))
    69.         return hit.distance - closerRadius;
    70.     else
    71.         return distance;
    72. }
    73.  
    74. static function ClampAngle (angle : float, min : float, max : float)
    75. {
    76.     if (angle < -360)
    77.         angle += 360;
    78.     if (angle > 360)
    79.         angle -= 360;
    80.     return Mathf.Clamp (angle, min, max);
    81. }
    82.  
    83. @script AddComponentMenu("Dead Space/Issac Camera")
    84.  
     
  2. thommoboy

    thommoboy

    Joined:
    Jul 20, 2012
    Posts:
    59
  3. dany

    dany

    Joined:
    Apr 5, 2011
    Posts:
    13
  4. SureSight

    SureSight

    Joined:
    Aug 2, 2013
    Posts:
    65
    I don't suppose you have a code snippet to share, Dany?
     
  5. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I put an answer in the comments of the video if it helps
     
  6. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I could probably give more help if you told me how to set it up.