Unity camera focus function, unity camera focus

Source: Internet
Author: User

Unity camera focus function, unity camera focus

Abstract: This article is original, reprint please indicate the source of http://www.cnblogs.com/AdvancePikachu/p/6742796.html

Requirements:

When editing a game's task editing scenario, the camera needs to switch back and forth between multiple objects to be edited. If you only use the camera's mobile rotation, for objects with relatively close locations, a focus function is required when the relative location is too far, you can easily automatically focus the camera on a relatively configurable position of the object to be edited.

:

If you have the focus function, you can easily focus the camera between the Cube and Sphere.

 1 public Transform _cube; 2     // Use this for initialization 3     void Start () { 4         _cube = GameObject.Find ("Cube").transform; 5         ViewSwitch.Instacne.GetRelativePosition (-10, 1, 0); 6     } 7      8     // Update is called once per frame 9     void Update () {10         if(Input.GetKey(KeyCode.Space))11             ViewSwitch.Instacne.GetData(this.transform,_cube);12     }

 

A simple call can achieve the following results:

 

 

Not much nonsense. Go to the Code:

1 // ===== AdvancePikachu 2017 ========= 2 // file function description; camera Focus 3 // create a representation; advancePikachu 4 // ========================================== ================================= 5 6 using UnityEngine; 7 using System. collections; 8 9 public class ViewSwitch: MonoBehaviour {10 11 private static ViewSwitch instance; 12 13 public static ViewSwitch Instacne {14 get {15 if (instance = null) {16 GameObject go = new GameObject (); 17 go. name = "ViewSwitch"; 18 instance = go. addComponent <ViewSwitch> (); 19} 20 return instance; 21} 22} 23 24 public float speed = 10f; 25 26 int method; 27 bool isBreakSwitch = true; 28 Transform startTransform; 29 Transform targetTransform; 30 Vector3 endPosition; 31 Quaternion endQuaternion; 32 Vector3 relativeEulers = new Vector3 (20, 0, 0); 33 Vector3 relativePositon = new Vector3 (-5 0, 30, 0); 34 35 // <summary> 36 // Gets the start transform. 37 /// </summary> 38 /// <returns> The start transform. </returns> 39 // <param name = "start"> Start. </param> 40 Transform GetStartTransform (Transform start) {41 startTransform = start; 42 return startTransform; 43} 44 45 // <summary> 46 // Gets the target transform. 47 /// </summary> 48 /// <returns> The target transform. </returns> 49 /// <Param name = "target"> Target. </param> 50 Transform GetTargetTransform (Transform target) {51 targetTransform = target; 52 GetEndPosition (); 53 GetEndQuaternion (); 54 return targetTransform; 55} 56 57 // <summary> 58 // Gets the relative position. 59 /// </summary> 60 // <returns> The relative position. </returns> 61 // <param name = "forword"> Forword. </param> 62 // <param name = "up"> Up. </par Am> 63 // <param name = "right"> Right. </param> 64 public Vector3 GetRelativePosition (float forword =-50f, float up = 30f, float right = 0f) {65 relativePositon = new Vector3 (forword, up, right ); 66 return relativePositon; 67} 68 69 // <summary> 70 // Gets the relative eulers. 71 /// </summary> 72 /// <returns> The relative eulers. </returns> 73 // <param name = "eulerX"> Euler x. </param> 74 // <param Name = "eulerY"> Euler y. </param> 75 // <param name = "eulerZ"> Euler z. </param> 76 public Vector3 outputs (float eulerX = 20f, float eulerY = 0, float eulerZ = 0) {77 relativeEulers = new Vector3 (eulerX, eulerY, eulerZ ); 78 return relativeEulers; 79} 80 81 // <summary> 82 // Gets the end position. 83 /// </summary> 84 /// <returns> The end position. </returns> 85 Vector3 GetEndPosition () {86 endPos Ition = targetTransform. position + 87 targetTransform. forward * relativePositon. x + 88 targetTransform. up * relativePositon. y + 89 targetTransform. right * relativePositon. z; 90 return endPosition; 91} 92 93 // <summary> 94 // Gets the end quaternion. 95 /// </summary> 96 /// <returns> The end quaternion. </returns> 97 Quaternion GetEndQuaternion () {98 endQuaternion = Quaternion. euler (targetTrans Form. eulerAngles + relativeEulers); 99 return endQuaternion; 100} 101 102 // <summary> 103 // Gets the data.104 // </summary> 105 // <param name = "start"> Start. </param> 106 // <param name = "target"> Target. </param> 107 // <param name = "I"> The index. </param> 108 public void GetData (Transform start, Transform target, int I = 0) {109 if (target! = Null) 110 isBreakSwitch = false; 111 else {112 isBreakSwitch = true; 113 return; 114} 115 116 GetStartTransform (start); 117 GetTargetTransform (target); 118 method = I; 119 if (I = 1) 120 ViewChangeImmediately (); 121} 122 123 // <summary> 124 // Views the change.125 // </summary> 126 void ViewChange () {127 if (! IsBreakSwitch) {128 startTransform. position = Vector3.Slerp (startTransform. position, endPosition, Time. deltaTime * speed); 129 startTransform. rotation = Quaternion. slerp (startTransform. rotation, endQuaternion, Time. deltaTime * speed); 130 131 if (Vector3.Distance (startTransform. position, endPosition) <= 0.5f & 132 Quaternion. angle (startTransform. rotation, endQuaternion) <= 0.5f) {133 Debug. log (" Camera Arrived at the specified location! "); 134 isBreakSwitch = true; 135} 136} else137 return; 138} 139 140 // <summary> 141 // Views the change immediately.142 // </summary> 143 void ViewChangeImmediately () {144 if (! IsBreakSwitch) {145 startTransform. position = Vector3.Slerp (startTransform. position, endPosition, Time. time); 146 startTransform. rotation = Quaternion. slerp (startTransform. rotation, endQuaternion, Time. time); 147 148 if (Vector3.Distance (startTransform. position, endPosition) <= 0.5f & 149 Quaternion. angle (startTransform. rotation, endQuaternion) <= 0.5f) {150 Debug. log ("Camera Arrived at the spec Ified location! "); 151 isBreakSwitch = true; 152} 153} else154 return; 155} 156 157 // <summary> 158 // Breaks the switch.159 // </summary> 160 // <returns> <c> true </c>, if switch was broken, <c> false </c> otherwise. </returns> 161 // <param name = "isBreak"> If set to <c> true </c> is break. </param> 162 public bool BreakSwitch (bool isBreak = true) {163 isBreakSwitch = isBreak; 164 return isBreakSwitch; 165} 166 167 void Update () {168 if (method = 0) 169 ViewChange (); 170} 171}View Code

I wrote two focus Methods: instant focus and time difference focus. You can choose what you need.

Here I made this script into a singleton. The understanding of Singleton is simple, so it looks simple. If you have a better Singleton method, please give me some advice, thank you !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.