Advertisement
Sinuousity

TPCameraAngleSnap.cs

Mar 1st, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. //TPCameraAngleSnap.cs
  2. //Script By Thomas Rasor
  3.  
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. public class TPCameraAngleSnap : MonoBehaviour
  8. {
  9.     public Transform target;
  10.     public float distance,height,speed,angleIncrement=45f;
  11.     public bool smoothRotate;
  12.    
  13.     //Here we store private variable for each variable we want to smoothly transition
  14.     //Variables have two versions, [c]urrent and [w]anted
  15.     private Vector3 cPos,wPos;
  16.     private float cDist,wDist,cYRot,wYRot;
  17.  
  18.     void Start ()
  19.     {
  20.         //Set the wanted distance to the distance from the inspector panel.
  21.         wDist = distance;
  22.     }//End Start
  23.  
  24.     void Update ()
  25.     {
  26.         //Here we just check for Inputs and wrap the new wanted y rotation values around 360 degrees
  27.         if (Input.GetKeyDown(KeyCode.LeftArrow))
  28.             wYRot = Mathf.Repeat(wYRot-angleIncrement,360f);
  29.         if (Input.GetKeyDown(KeyCode.RightArrow))
  30.             wYRot = Mathf.Repeat(wYRot+angleIncrement,360f);
  31.  
  32.         if (smoothRotate)
  33.         {
  34.             //Here's where we smooth the rotation and position values IF smoothRotate is true
  35.             cYRot = Mathf.LerpAngle(cYRot,wYRot,Time.deltaTime*speed);
  36.             cPos = Vector3.Lerp(cPos,wPos,Time.deltaTime*speed);
  37.         }
  38.         else
  39.         {
  40.             //Otherwise we just set the current rotation and position to the wanted rotation and position
  41.             cYRot = wYRot;
  42.             cPos = wPos;
  43.         }
  44.        
  45.         //We always smooth the distance but you might not want to, if that is the case just do this as above
  46.         cDist = Mathf.Lerp(cDist,wDist,Time.deltaTime*speed);
  47.  
  48.         //Here we calculate the new position using the formula used to get a point on the circumference of a
  49.         //circle:
  50.         //x = cos(angle * (PI/180)) + origin.x;
  51.         //z = sin(angle * (PI/180)) + origin.z;
  52.         wPos.x = (cDist * Mathf.Cos(cYRot * Mathf.PI / 180f)) + target.position.x;
  53.         wPos.z = (cDist * Mathf.Sin(cYRot * Mathf.PI / 180f)) + target.position.z;
  54.         //And then set the wanted y position to the height
  55.         wPos.y = target.position.y + height;
  56.        
  57.         //Here we actually apply the calculations to the object's/camera's transform
  58.         transform.position = cPos;
  59.         //This just rotates the camera to look at target.position. We could do this other ways for more
  60.         //custom rotation, but this works for this senario.
  61.         transform.LookAt(target.position);
  62.     }//End Update
  63. }//End TPCameraAngleSnap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement