unity implements multi touch code

  • 2021-01-18 06:37:20
  • OfStack

This is what I saw on the forum, unity multi-touch. Feelings are good, share with everyone


// Used to bind a reference object 
var target : Transform;
// The zoom factor 
var distance = 10.0;
// Sliding speed left and right 
var xSpeed = 250.0;
var ySpeed = 120.0;
// Scaling limit factor 
var yMinLimit = -20;
var yMaxLimit = 80;
// The location of the camera 
var x = 0.0;
var y = 0.0;
// On the record 1 The second touch position of the phone determines whether the user is zooming in or out on the left 
private var oldPosition1 : Vector2;
private var oldPosition2 : Vector2;
 
// Initialize the game information Settings 
function Start () {
  var angles = transform.eulerAngles;
  x = angles.y;
  y = angles.x;
 
  // Make the rigid body not change rotation
  if (rigidbody)
    rigidbody.freezeRotation = true;
}
 
function Update ()
{
  // Determine the number of touches as a single touch 
  if(Input.touchCount == 1)
  {
    // The touch type is a moving touch 
    if(Input.GetTouch(0).phase==TouchPhase.Moved)
    {
      // Calculated by touch points X with Y location 
      x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
      y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 
    }
  }
 
  // Determine the number of touches to be multi-touches 
  if(Input.touchCount >1 )
  {
    // The first two finger touch types are both moving touches 
    if(Input.GetTouch(0).phase==TouchPhase.Moved¦¦Input.GetTouch(1).phase==TouchPhase.Moved)
    {
        // Calculates the current position of the two touch points 
        var tempPosition1 = Input.GetTouch(0).position;
        var tempPosition2 = Input.GetTouch(1).position;
        // The function returns true to zoom in and false to zoom out 
        if(isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2))
        {
          // Amplification factor over 3 No further amplification is allowed 
          // The data here is adjusted according to the model in my project, you can modify it yourself 
          if(distance > 3)
          {
            distance -= 0.5;
          }
        }else
        {
          // Minimize wash and return 18.5 No further reduction is allowed 
          // The data here is adjusted according to the model in my project, you can modify it yourself 
          if(distance < 18.5)
          {
            distance += 0.5;
          }
        }
      // On the backup 1 The position of the sub-touch point for comparison 
      oldPosition1=tempPosition1;
      oldPosition2=tempPosition2;
    }
  }
}
 
// The function returns true to zoom in and false to zoom out 
function isEnlarge(oP1 : Vector2,oP2 : Vector2,nP1 : Vector2,nP2 : Vector2) : boolean
{
  // The function is passed in 1 The position of the two points of the second touch and the position of the two points of the current touch are used 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)
  {
     // Zoom gestures 
     return true;
  }else
  {
    // Smaller gestures 
    return false;
  }
}
 
//Update methods 1 Once the call is over go in here and figure out where to reset the camera 
function LateUpdate () {
 
  //target For our bound box variable, scale the rotated reference 
  if (target) {    
 
    // Reset the camera 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 < -360)
    angle += 360;
  if (angle > 360)
    angle -= 360;
  return Mathf.Clamp (angle, min, max);
}

The above is the entire content of this article, there is a need for small partners can refer to the next


Related articles: