|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +public class CameraAngleSwitch : MonoBehaviour { |
| 6 | + public string bName; |
| 7 | + |
| 8 | + public List<Vector3> rotations = new List<Vector3>(1); |
| 9 | + public List<Vector3> positions = new List<Vector3>(1); |
| 10 | + |
| 11 | + public bool instantSwitch = false; |
| 12 | + public float lerpSpeed; |
| 13 | + int arrIndex = 0; |
| 14 | + Coroutine inst; |
| 15 | + // Use this for initialization |
| 16 | + void Start () { |
| 17 | + if(rotations[0] == Vector3.zero && positions[0] == Vector3.zero) |
| 18 | + { |
| 19 | + rotations[0] = transform.rotation.eulerAngles; |
| 20 | + positions[0] = transform.position; |
| 21 | + } |
| 22 | + if (rotations.Count != positions.Count) |
| 23 | + { |
| 24 | + print("ROTATIONS AND POSITIONS ARRAYS NOT EQUAL"); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // Update is called once per frame |
| 29 | + void Update () { |
| 30 | + if (Input.GetButtonDown(bName)) |
| 31 | + { |
| 32 | + if(arrIndex< rotations.Count-1) |
| 33 | + { |
| 34 | + arrIndex++; |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + arrIndex = 0; |
| 39 | + } |
| 40 | + |
| 41 | + if (!instantSwitch) |
| 42 | + { |
| 43 | + if (inst != null) |
| 44 | + { |
| 45 | + StopCoroutine(inst); //stop any previous movement; |
| 46 | + } |
| 47 | + inst = StartCoroutine(LerpTo(positions[arrIndex], rotations[arrIndex], lerpSpeed)); |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + transform.position = positions[arrIndex]; |
| 55 | + transform.eulerAngles = rotations[arrIndex]; |
| 56 | + |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public IEnumerator LerpTo(Vector3 pos, Vector3 rot, float time) |
| 62 | + { |
| 63 | + float elapsedTime = 0; |
| 64 | + |
| 65 | + while (elapsedTime < time) |
| 66 | + { |
| 67 | + transform.position = Vector3.Lerp(transform.position, pos, elapsedTime/time); |
| 68 | + transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, rot, elapsedTime / time); |
| 69 | + elapsedTime += Time.deltaTime; |
| 70 | + yield return new WaitForEndOfFrame(); |
| 71 | + |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public void AddCoordinates() |
| 76 | + { |
| 77 | + positions.Add(transform.position); |
| 78 | + rotations.Add(transform.eulerAngles); |
| 79 | + } |
| 80 | + public void DeleteLastCoordinates() |
| 81 | + { |
| 82 | + positions.RemoveAt(positions.Count-1); |
| 83 | + rotations.RemoveAt(positions.Count-1); |
| 84 | + |
| 85 | + } |
| 86 | +} |
0 commit comments