Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TPSCamera : MonoBehaviour {
- public Transform pivot;
- public float minRadius = 5;
- public float maxRadius = 15;
- public float minAzimuth = 0;
- public float maxAzimuth = 360;
- public float minElevation = 5;
- public float maxElevation = 180;
- public float resetTime = 5;
- public float radius = 0;
- public float azimuth = 180;
- public float elevation = 60;
- float defoAzimuth, defoElevation;
- bool isMove;
- float timer;
- // Use this for initialization
- void Start () {
- defoAzimuth = azimuth;
- defoElevation = elevation;
- radius = (minRadius + maxRadius) / 2;
- updatePosition();
- }
- // Update is called once per frame
- void Update () {
- var horizon = -Input.GetAxis("Mouse X");
- var vertical = Input.GetAxis("Mouse Y");
- if (timer > resetTime) {
- azimuth = Mathf.Lerp(azimuth, defoAzimuth, Time.deltaTime);
- elevation = Mathf.Lerp(elevation, defoElevation, Time.deltaTime);
- } else {
- azimuth += horizon;
- elevation += vertical;
- }
- azimuth = Mathf.Clamp(azimuth, minAzimuth, maxAzimuth);
- elevation = Mathf.Clamp(elevation, minElevation, maxElevation);
- var wheel = Input.GetAxis("Mouse ScrollWheel");
- radius += wheel;
- radius = Mathf.Clamp(radius, minRadius, maxRadius);
- if (Mathf.Abs(horizon) > 0 || Mathf.Abs(vertical) > 0 || Mathf.Abs(wheel) > 0) {
- isMove = true;
- } else {
- isMove = false;
- }
- if (!isMove) {
- timer += Time.deltaTime;
- } else {
- timer = 0;
- }
- }
- void LateUpdate() {
- transform.position = updatePosition();
- transform.LookAt(pivot);
- }
- Vector3 updatePosition() {
- var offs = 90;
- var sinA = Mathf.Sin((azimuth + offs) * Mathf.Deg2Rad);
- var cosA = Mathf.Cos((azimuth + offs) * Mathf.Deg2Rad);
- var sinE = Mathf.Sin(elevation * Mathf.Deg2Rad);
- var cosE = Mathf.Cos(elevation * Mathf.Deg2Rad);
- return pivot.position + new Vector3(sinE * cosA, cosE, sinE * sinA) * radius;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement