using UnityEngine; /// /// Example script using the Singleton generic class. This object /// rotates a camera around an object. /// public class MyScript : MonoBehaviour { #region Properties /// /// Camera being moved /// public Camera targetCamera; /// /// Object camera is moving around /// public Transform target; /// /// Number of seconds to take to revolve around the target object. /// public float duration = 5f; /// /// Number of units away from object to place camera. /// public float distance = 10f; #endregion #region Events /// /// We're assigning the singleton during awake, since its the /// earliest access we have to the MonoBehaviour object instance. /// Note that the .instance property is write-once, meaning that /// once it has an instance additional calls will not overwrite /// the original reference. /// void Awake() { Singleton.instance = this; } /// /// Use the active camera by default, and the game object attached to /// this component to look at. /// void Start() { this.targetCamera = Camera.main; this.target = this.transform; } /// /// Updates object position. I'm using trig functions here, but /// you could use a quaternion to achieve the same effect. /// void Update() { this.theta += Time.deltaTime / this.duration; Vector3 offset = new Vector3(); offset.x = Mathf.Cos(this.theta) * this.distance; offset.y = Mathf.Sin(this.theta) * this.distance; offset.z = 0; Vector3 v = this.target.position + offset; this.targetCamera.transform.position = v; this.targetCamera.transform.LookAt(this.target); } #endregion #region Private /// /// Current angle around target. /// private float theta = 0f; #endregion }