Control Model display perspective

Source: Internet
Author: User
 

 

1 using unityengine; 2 using system. collections; 3 4 public class cameracontrol: monobehaviour {5 Public transform target; 6 public float distance = 5f; // scaling factor 7 public float mixdistance = 2; // The nearest distance from the camera to the object. The smaller the camera is, the larger the magnification is. 8 public float maxdistance = 10. // the maximum distance from the camera to the object, the larger the scale-out, the higher the 9 10 public float xspeed = 2500000f; 11 public float yspeed = 1200000f; 12 13 public float yminlimit =-80; // angle limiting factor 14 public float ymaxlimit = 80; 15 16 public float x = 0.0f; // camera position 17 public float y = 0.0f; 18 private vector2 oldposition1; 19 private vector2 oldposition2; 20 // use this for initialization 21 void start () {22 vector3 angles = transform. eulerangles; 23 x = angles. y; 24 y = angles. x; 25 // This. transform. position = target. transform. position + new vector3 (0, 0, 2); // assign the camera position 26 27 // make the rigid body not change rotation 28 If (getcomponent <Rigidbody> ()) 29 getcomponent <Rigidbody> (). freezerotation = true; 30} 31 32 33 // update is called once per frame 34 void Update () 35 {36 IF (input. touchcount = 1) 37 {38 39 if (input. gettouch (0 ). phase = touchphase. moved) 40 {41 42 X + = input. getaxis ("Mouse X") * xspeed * 0.02f; 43 y-= input. getaxis ("Mouse y") * yspeed * 0.02f; 44 45} 46} 47 48 49 If (input. touchcount> 1) 50 {51 52 If (input. gettouch (0 ). phase = touchphase. moved | input. gettouch (1 ). phase = touchphase. moved) 53 {54 55 var tempposition1 = input. gettouch (0 ). position; 56 var tempposition2 = input. gettouch (1 ). position; 57 if (isenlarge (oldposition1, oldposition2, tempposition1, tempposition2) 58 {59 If (distance> mixdistance) 60 {61 distance-= 0.5f; 62} 63} 64 else 65 {66 If (distance <maxdistance) 67 {68 distance + = 0.5f; 69} 70} 71 oldposition1 = tempposition1; 72 oldposition2 = tempposition2; 73} 74} 75} 76 bool isenlarge (vector2 OP1, vector2 OP2, vector2 Np1, vector2 NP2) 77 {78 var leng1 = mathf. SQRT (op1.x-op2.x) * (op1.x-op2.x) + (op1.y-op2.y) * (op1.y-op2.y); 79 var leng2 = mathf. SQRT (np1.x-np2.x) * (np1.x-np2.x) + (np1.y-np2.y) * (np1.y-np2.y); 80 If (leng1 <leng2) 81 {82 return true; 83} else 84 {85 return false; 86} 87} 88 void lateupdate () 89 {90 if (target) 91 {92 93 y = clampangle (Y, yminlimit, ymaxlimit ); 94 var rotation = Quaternion. euler (Y, X, 0); 95 var position = rotation * New vector3 (0.0f, 0.0f,-distance) + target. position; 96 97 transform. rotation = rotation; 98 transform. position = position; 99} 100} 101 static float clampangle (float angle, float min, float max) 102 {103 If (angle <-360) 104 angle + = 360; 105 if (angle> 360) 106 angle-= 360; 107 return mathf. clamp (angle, Min, max); 108} 109}

 

Reference http://www.xuanyusong.com/archives/512

Control Model display perspective

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.