Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TPCameraAngleSnap.cs
- //Script By Thomas Rasor
- using UnityEngine;
- using System.Collections;
- public class TPCameraAngleSnap : MonoBehaviour
- {
- public Transform target;
- public float distance,height,speed,angleIncrement=45f;
- public bool smoothRotate;
- //Here we store private variable for each variable we want to smoothly transition
- //Variables have two versions, [c]urrent and [w]anted
- private Vector3 cPos,wPos;
- private float cDist,wDist,cYRot,wYRot;
- void Start ()
- {
- //Set the wanted distance to the distance from the inspector panel.
- wDist = distance;
- }//End Start
- void Update ()
- {
- //Here we just check for Inputs and wrap the new wanted y rotation values around 360 degrees
- if (Input.GetKeyDown(KeyCode.LeftArrow))
- wYRot = Mathf.Repeat(wYRot-angleIncrement,360f);
- if (Input.GetKeyDown(KeyCode.RightArrow))
- wYRot = Mathf.Repeat(wYRot+angleIncrement,360f);
- if (smoothRotate)
- {
- //Here's where we smooth the rotation and position values IF smoothRotate is true
- cYRot = Mathf.LerpAngle(cYRot,wYRot,Time.deltaTime*speed);
- cPos = Vector3.Lerp(cPos,wPos,Time.deltaTime*speed);
- }
- else
- {
- //Otherwise we just set the current rotation and position to the wanted rotation and position
- cYRot = wYRot;
- cPos = wPos;
- }
- //We always smooth the distance but you might not want to, if that is the case just do this as above
- cDist = Mathf.Lerp(cDist,wDist,Time.deltaTime*speed);
- //Here we calculate the new position using the formula used to get a point on the circumference of a
- //circle:
- //x = cos(angle * (PI/180)) + origin.x;
- //z = sin(angle * (PI/180)) + origin.z;
- wPos.x = (cDist * Mathf.Cos(cYRot * Mathf.PI / 180f)) + target.position.x;
- wPos.z = (cDist * Mathf.Sin(cYRot * Mathf.PI / 180f)) + target.position.z;
- //And then set the wanted y position to the height
- wPos.y = target.position.y + height;
- //Here we actually apply the calculations to the object's/camera's transform
- transform.position = cPos;
- //This just rotates the camera to look at target.position. We could do this other ways for more
- //custom rotation, but this works for this senario.
- transform.LookAt(target.position);
- }//End Update
- }//End TPCameraAngleSnap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement