Chaotic Motion  1.0
CameraSpin.cs
1 using UnityEngine;
2 using System.Collections;
3 
11 public class CameraSpin : MonoBehaviour {
12 
14  public float spinRate = 1f;
15 
16  private Transform mainCamera;
17 
18  public float boomStep = 2f;
19 
20  private const float boomMin = 2f;
21  private const float boomMax = 1000f;
22 
23  // Use this for initialization
24  void Start () {
25  // assume first child is the camera
26  mainCamera = transform.GetChild(0);
27  }
28 
29  // Update is called once per frame
30  void Update () {
31  float boomLength = mainCamera.localPosition.z;
32 
33  if (Input.GetKey(KeyCode.RightArrow)) {
34  transform.rotation *= Quaternion.AngleAxis( spinRate, Vector3.up);
35  } else if (Input.GetKey(KeyCode.LeftArrow)) {
36  transform.rotation *= Quaternion.AngleAxis( -spinRate, Vector3.up);
37  } else if (Input.GetKey(KeyCode.UpArrow)) {
38  transform.rotation *= Quaternion.AngleAxis( spinRate, Vector3.right);
39  } else if (Input.GetKey(KeyCode.DownArrow)) {
40  transform.rotation *= Quaternion.AngleAxis( -spinRate, Vector3.right);
41  } else if (Input.GetKey(KeyCode.Equals)) {
42  // shorten the boom
43  boomLength -= boomStep;
44  boomLength = Mathf.Max(boomLength, boomMin);
45  Vector3 localP = mainCamera.localPosition;
46  localP.z = boomLength;
47  mainCamera.localPosition = localP;
48 
49  } else if (Input.GetKey(KeyCode.Minus)) {
50  // lengthen the boom
51  boomLength += boomStep;
52  boomLength = Mathf.Min(boomLength, boomMax);
53  Vector3 localP = mainCamera.localPosition;
54  localP.z = boomLength;
55  mainCamera.localPosition = localP;
56  }
57  }
58 }
Key control to rotate camera boom using Arrow keys. Scale with +/-.
Definition: CameraSpin.cs:11
float spinRate
Rate of spin (degrees per Update)
Definition: CameraSpin.cs:14