"Reprint" Unity3d Research Institute's iOS touch screen gesture controls lens rotation and zooming

Source: Internet
Author: User

The previous articles introduced a number of problems with the Unity3d engine itself, and today we are going back to the iOS device to discuss some touch screen gestures, the goal of this chapter is to achieve the model's rotation by touching the iphone screen gesture, and scaling the model.

Let's think about the rotation of the model, actually the rotation of the lens. The scale of the model is actually the coordinates of the z-axis direction of the lens. The realization of this chapter is only necessary to control the location of the lens.

We create a simple game plane and then place a box in the plane as a reference for rotating and zooming. As shown, select the camera and add a script name of move to the camera. The script has a parameter Target, its role is to set the camera rotation mobile reference, here a box assigned to Target, then the left and right sliding screen will find the box in the rotation, the hands of the zoom screen will find the box is enlarged and reduced.

 
    //used to bind referential objects     PublicTransform Target; //Zoom Factor    floatDistance =10.0f; //left and right sliding movement speed    floatXSpeed =250.0f; floatYspeed =120.0f; //scaling Limit factor    floatYminlimit =-20f; floatYmaxlimit =80f; //location of the camera    floatx =0.0f; floaty =0.0f; //record the last phone touch position to determine whether the user is zooming in or out on the left hand    PrivateVector2 OldPosition1; PrivateVector2 OldPosition2; //Initializing game information Settings    voidStart () {//eulerangles (Euler angle): The x, y, and z angles represent the rotation of the Z-degree around the z-axis, rotate the X-degree around the x-axis, and rotate the Y-degree (in this order) around the y-axis.         varangles = transform.eulerangles;//that is, the relative rotation value of the camerax =Angles.y; Y=angles.x; //Make the rigid body isn't change rotation        if(rigidbody) rigidbody.freezerotation=true;//freeze rotation, if freezerotation is enabled, the rotation will not be modified by the object simulation. This is useful when creating a first-person shooter because the player needs to use the mouse to fully control the rotation.     }    voidUpdate () {//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 + = (float) (Input.getaxis ("Mouse X") * XSpeed *0.02); Y-= (float) (Input.getaxis ("Mouse Y") * Yspeed *0.02); }        }        //Judging the number of touches as multi-touch        if(Input.touchcount >1)        {            //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.5f; }                }                Else                {                    //reduction factor returns 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.5f; }                }                //Backup the position of the last touch point to compareOldPosition1 =TempPosition1; OldPosition2=TempPosition2; }        }    }    //function returns true to enlarge, return false to shrink    BOOLIsenlarge (Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) {//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//var leng1 = mathf.sqrt ((op1.x-op2.x) * (op1.x-op2.x) + (OP1.Y-OP2.Y) * (OP1.Y-OP2.Y)); //var leng2 = mathf.sqrt ((np1.x-np2.x) * (np1.x-np2.x) + (NP1.Y-NP2.Y) * (NP1.Y-NP2.Y));        varLeng1 =vector2.distance (oP1, oP2); varLeng2 =vector2.distance (nP1, nP2); 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.    voidlateupdate () {//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 *NewVector3 (0.0f,0.0f,-distance) +target.position; //rotation. Toangleaxis (out Zwhangle, out Zwhaxis);            Converts a rotation with a "angle-axis" representation. //position indicates that the camera object's position is set to new Vector3 (0.0f, 0.0f,-distance), and then the zwhangle angle is rotated around the zwhaxis axis from this position, thus obtaining a fresh position. Adding target.position is to make the camera object's position farther away from the Zwhaxis axis//transform.rotation = rotation;//This sentence can be the same in the following wayTransform.position =position; Transform.rotation= Quaternion.lookrotation (Target.position-transform.position); }    }    Static floatClampangle (floatAnglefloatMinfloatmax) {        if(Angle <- the) Angle+= the; if(Angle > the) Angle-= the; returnmathf.clamp (angle, Min, max); }

Let's look at the move this script to illustrate a few important:

These methods are the methods that the system calls itself

function Start (): Only once when the game is started, it can be used for initialization of the script.

function Update (): The Start () method call is called at the end of each frame, where you can update the game logic.

function Lateupdate (): The Start () method call is called at the end of each frame, but it is called after the Update () call is complete.

: Http://vdisk.weibo.com/s/abssL
    • This article fixed link: http://www.xuanyusong.com/archives/512
    • Reprint Please specify: Rain pine Momo May 01, 2012 Yu Yussong Momo Program Research Institute published

The key code above is:

var position = rotation * New Vector3 (0.0f, 0.0f,-distance) + target.position;

I have already commented on the explanation! Do not understand can see another, to help understand:
 PublicGameobject Cameraobject;  Public floatCameradistance;//The distance the camera should is palced from the Palyer     Public floatCameraheight;//How heigh The camera should be    Private floatCameraangletoplayer;//The current angle the camera was to the Palyer    PrivateVector3 Tempvector;//The temporary vector we shall use for calcuations    voidUpdate () {Tempvector=Vector3.left; if(Input.getkey (" Left"))//rotation the angle based on input{Cameraangletoplayer= Cameraangletoplayer-(Time.deltatime *50f); }        Else if(Input.getkey (" Right") ) {Cameraangletoplayer= Cameraangletoplayer + (Time.deltatime *50f); } tempvector= Quaternion.angleaxis (Cameraangletoplayer, vector3.up) *Tempvector; Debug.drawline (transform.position, Tempvector*10f, Color.yellow); //cameraObject.transform.position = transform.position + (tempvector.normalized * cameradistance);CameraObject.transform.position = Tempvector;//Tempvector indicates that the location of the camera object at this time will be from Vector3.left ( -1f,0,0) This position rotates the cameraangletoplayer angle around the vector3.up axis to obtain a new position, multiplied by 10, to make the camera object's position farther from the vector3.up axisCameraObject.transform.rotation = Quaternion.lookrotation (Transform.position-cameraObject.transform.position); }

Reference: http://www.cnblogs.com/88999660/archive/2013/08/16/3262656.html

"Reprint" Unity3d Research Institute's iOS touch screen gesture controls lens rotation and zooming

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.