Unity3d lens moves with touch screen

Source: Internet
Author: User


#pragma strict//used to bind referential objectsvarTarget:transform; //Zoom FactorvarDistance = 10.0; //left and right sliding movement speedvarXSpeed = 250.0; varYspeed = 120.0; //scaling Limit factorvarYminlimit =-20; varYmaxlimit = 80; //location of the cameravarx = 0.0; vary = 0.0; //record the last phone touch position to determine whether the user is zooming in or out on the left handPrivatevarOldposition1:vector2; PrivatevarOldposition2:vector2; //Initializing game information SettingsfunctionStart () {varAngles =Transform.eulerangles; X=Angles.y; Y=angles.x; //Make the rigid body isn't change rotation    if(rigidbody) rigidbody.freezerotation=true; }         varNewobject:transform; functionUpdate () {//Judging the number of touches as a single touch    if(Input.touchcount = = 1)       {           //Touch type for mobile touch        if(Input.gettouch (0). phase==touchphase.moved) {//calculates x and Y positions based on touch pointsx + = Input.getaxis ("Mouse x") * xspeed * 0.01; Y-= Input.getaxis ("Mouse Y") * yspeed * 0.01; //adjust position and orientation by lens distance            varrotation = Quaternion.euler (-y, x, 0); Transform.rotation=rotation; }       }              //Judging the number of touches as multi-touch    if(Input.touchcount >1 )       {               varN:transform =instantiate (newobject,transform.position,transform.rotation); varFwd:vector3 =transform.            Transformdirection (Vector3.forward); N.rigidbody.addforce (Fwd*2800); //the first two finger touch types are mobile touch        if(Input.gettouch (0). phase==touchphase.moved| | Input.gettouch (1). phase==touchphase.moved) {//calculates the position of the current two-point touch point                varTempPosition1 = Input.gettouch (0). Position; varTempPosition2 = Input.gettouch (1). Position; //function returns true to enlarge, return false to shrink                if(Isenlarge (Oldposition1,oldposition2,tempposition1,tempposition2)) {//no further amplification is allowed after the magnification factor exceeds 3                    //The data here is adjusted according to the model in my project, and you can modify it yourself .                    if(Distance > 3) {Distance-= 0.5; }                    }Else                   {                       //Shrink wash back after 18.5 not allowed to continue shrinking                    //The data here is adjusted according to the model in my project, and you can modify it yourself .                    if(Distance < 18.5) {Distance+ = 0.5; }                   }               //Backup the position of the last touch point to compareoldposition1=TempPosition1; OldPosition2=TempPosition2; }       }   }            //function returns true to enlarge, return false to shrinkfunctionIsenlarge (OP1:VECTOR2,OP2:VECTOR2,NP1:VECTOR2,NP2:VECTOR2):Boolean   {       //The function passes the position of the last touch two point and the position of the touch two points to calculate the user's gesture    varLeng1 =mathf.sqrt ((op1.x-op2.x) * (op1.x-op2.x) + (OP1.Y-OP2.Y) * (op1.y-op2.y)); varLeng2 =mathf.sqrt ((np1.x-np2.x) * (np1.x-np2.x) + (NP1.Y-NP2.Y) * (np1.y-np2.y)); if(leng1<leng2) {            //Zoom gesture         return true; }Else       {           //Zoom Out gesture        return false; }   }      //Update method Once the call is over, enter here to figure out where to reset the camera.functionlateupdate () {//target is the box variable we are bound to, scaling the rotation of the reference    if(target) {//resetting the camera's locationy =clampangle (y, Yminlimit, ymaxlimit); varrotation = Quaternion.euler (y, x, 0); varPosition = rotation * VECTOR3 (0.0, 0.0,-distance) +target.position; //transform.rotation = rotation;         //transform.position = position; }} staticfunctionClampangle (angle:float, min:float, Max:float) {       if(Angle <-360) Angle+ = 360; if(Angle > 360) Angle-= 360; returnmathf.clamp (angle, Min, max); }   
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.