using UnityEngine; using System.Collections; public class CameraClass : MonoBehaviour { public Transform target; private const float deg = Mathf.Deg2Rad; private const float distance = 5.0f; //Lateral distance of camera from player. private const float zoomMax = 10f, zoomMin = 4f; //Min-Max camera zoom levels private float zoomTarget = 8, zoomVel = 0; //Camera rotation variables private float angle = 0, targetangle = 0.0f, currentVel = 0.0f; private float yoffset = 1.8f, targetHeight = 0.0f, curHeight = 0.0f, verticalVel = 0.0f; private const float DEFAULT_ANGLE = 45; private const float DEFAULT_ZOOM = 8; private const float twoPi = Mathf.PI * 2; private const float deg45 = Mathf.PI / 4; private Vector3 point; private bool allowSmoothRot; //True when camera turn key is still held after initial rotation void Start () { angle = deg45; targetangle = angle; point = target.transform.position; curHeight = point.y; targetHeight = curHeight; transform.LookAt(new Vector3(point.x, point.y - 0.5f, point.z)); } void Update () { //Camera Reset if(Input.GetButtonDown("Camera Reset")) { if(angle <= deg*180 && angle != deg*DEFAULT_ANGLE) targetangle = deg*DEFAULT_ANGLE; else if(angle > deg*180 && angle != deg*DEFAULT_ANGLE) targetangle = deg*(360 + DEFAULT_ANGLE); zoomTarget = DEFAULT_ZOOM; } //Rotate Camera 45 degrees on button press if(Input.GetButtonDown("Camera Left")) { targetangle += deg45; allowSmoothRot = false; } if(Input.GetButtonDown("Camera Right")) { targetangle -= deg45; allowSmoothRot = false; } //After initial rotation-- //Move the camera left and right at a static speed, and update the target angle accordingly if(Input.GetButton("Camera Left")) { if(allowSmoothRot) angle += deg*1f; if(angle >= targetangle - deg*5) { targetangle += deg45; allowSmoothRot = true; } } else if(Input.GetButton("Camera Right")) { if(allowSmoothRot) angle -= deg*1f; if(angle <= targetangle + deg*5) { targetangle -= deg45; allowSmoothRot = true; } } //Move the camera toward/away from the player if(Input.GetButton("Camera Zoom In") && zoomTarget > zoomMin) { if(GetComponent().orthographicSize < zoomTarget + 0.25f) zoomTarget -= 2f; } else if(Input.GetButton("Camera Zoom Out") && zoomTarget < zoomMax) { if(GetComponent().orthographicSize > zoomTarget - 0.25f) zoomTarget += 2f; } if(GetComponent().orthographicSize != zoomTarget) { GetComponent().orthographicSize = Mathf.SmoothDamp(GetComponent().orthographicSize, zoomTarget, ref zoomVel, .1f, 8f, Time.deltaTime); if(GetComponent().orthographicSize < zoomTarget+.001f && GetComponent().orthographicSize > zoomTarget-.001f) { GetComponent().orthographicSize = zoomTarget; } } updateCamera(true); } public void updateCamera(bool applyRotation) { //If the camera rotation keys are not being pressed, if(!allowSmoothRot || (!Input.GetButton("Camera Left") && !Input.GetButton("Camera Right"))) { if(angle != targetangle && applyRotation) { //Decelerate rotation toward target angle until it is reached angle = Mathf.SmoothDampAngle(angle, targetangle, ref currentVel, .2f, 8f, Time.deltaTime); } //Correct the floating point number when within a small range of the target angle if(angle < targetangle + 0.001f && angle > targetangle - 0.001f) { angle = targetangle; angle = Mathf.Repeat(angle, twoPi);//Keeps the angle variable between 0 and twoPi targetangle = angle; allowSmoothRot = false; } } //Follow Player point = target.transform.position; float xoffset = Mathf.Sin(angle) * distance; float zoffset = Mathf.Cos(angle) * distance; targetHeight = point.y; curHeight = Mathf.SmoothDamp(curHeight, targetHeight, ref verticalVel, .5f, 16f, Time.deltaTime); transform.position = new Vector3(point.x + xoffset, curHeight + GetComponent().orthographicSize / yoffset, point.z + zoffset); transform.LookAt(new Vector3(point.x, curHeight - 0.5f, point.z)); } //Returns a relative angle in Radians where "steps" is the number of //45 degree turns clockwise from "toward" the camera public float getDirection(int steps) { return Mathf.Repeat(this.angle + (deg45 * steps), twoPi); } public float getAngle() { return angle; } }