Unity Multi-Touch (zoom in, zoom out)

Source: Internet
Author: User

Used to bind reference object Var Target:transform;  Scaling factor var distance = 10.0;  Left and right sliding movement speed var xspeed = 250.0;  var yspeed = 120.0;  Scaling Limit factor var yminlimit =-20;  var ymaxlimit = 80;  Location of the camera var x = 0.0;  var y = 0.0;  Record the last time the phone touch location to determine whether the user is in the left or zoom out gesture private Var Oldposition1:vector2;    private Var Oldposition2:vector2;      Initialize game information set function Start () {var angles = transform.eulerangles;      x = Angles.y;        y = angles.x;  Make the rigid body isn't change rotation if (rigidbody) rigidbody.freezerotation = true; } function Update () {//To determine the number of touches is a single touch if (Input.touchcount = = 1) {//Touch type is mobile touch if (Input . Gettouch (0). phase==touchphase.moved) {//calculate x and Y positions based on touch points x + = Input.getaxis ("Mouse x") * x              Speed * 0.02;            Y-= Input.getaxis ("Mouse y") * yspeed * 0.02; }}//To determine the number of touches for multi-touch if (Input.touchcount >1) {//////Two finger touch type are mobile touch if (Input.gettouc H (0). phase==touchphase.moved| | Input.gettouch (1). phase==touchphase.moved) {//calculates the position of the current two-point Touch point var tempPosition1                  = Input.gettouch (0). Position;                  var tempPosition2 = Input.gettouch (1). Position;                       function returns true to enlarge, return false for narrowing if (Isenlarge (Oldposition1,oldposition2,tempposition1,tempposition2)) { Amplification factor of more than 3 is not allowed to continue to enlarge//The data here is adjusted according to the model in my project, you can modify if (dis                      Tance > 3) {distance-= 0.5; }}else {//shrink wash back after 18.5 is not allowed to continue shrinking//The data here is based on my project Model and adjust, you can modify if (distance < 18.5) {distance + =                      0.5;              }}//backup the position of last touch point, used to compare oldposition1=tempposition1; Oldposition2=temppoSition2;       }}}//function return True to zoom in, return false for Shrink function Isenlarge (OP1:VECTOR2,OP2:VECTOR2,NP1:VECTOR2,NP2:VECTOR2): boolean { The function passes the position of the last touch two points with 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));      if (leng1<leng2) {//magnification gesture return true;      }else {//Zoom out gesture return false;                   }}//update method Once the call is completed enter here to calculate the location of the Reset camera function lateupdate () {//target is the box variable that we bound, scaling the rotation of the reference if (target) {          Reset the camera's position y = Clampangle (y, Yminlimit, ymaxlimit);          var rotation = Quaternion.euler (y, x, 0);            var position = rotation * VECTOR3 (0.0, 0.0,-distance) + target.position;          Transform.rotation = rotation;      Transform.position = position; }} static function Clampangle (Angle:float, Min:float, max:float) {if (Angle &Lt      -360) Angle + = 360;      if (angle > Angle)-= 360;  return Mathf.clamp (angle, Min, max);   }


Unity Multi-Touch (zoom in, zoom out)

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.