Unity releases Android finger to control object scaling and rotation

Source: Internet
Author: User

Just to help the body.

Using Unityengine;

Using System.Collections;
Using System.IO;
public class Hehe:monobehaviour
{
Private Touch oldTouch1; Last touch point 1 (finger 1)
Private Touch OLDTOUCH2; Last touch point 2 (finger 2)

void Start ()
{
}

void Update ()
{
No touch
if (input.touchcount <= 0)
{
Return
}
Single touch, horizontal up/down rotation
if (1 = = Input.touchcount)
{
Touch touch = Input.gettouch (0);
Vector2 deltapos = touch.deltaposition;
Transform. Rotate (Vector3.down * deltapos.x, Space.world);
Transform. Rotate (Vector3.right * deltapos.y, Space.world);
}
Multi-touch, zoom-in and zoom out
Touch newTouch1 = Input.gettouch (0);
Touch NEWTOUCH2 = Input.gettouch (1);
The 2nd just starts to touch the screen, only records, does not do the processing
if (newtouch2.phase = = Touchphase.began)
{
OLDTOUCH2 = NEWTOUCH2;
OldTouch1 = NewTouch1;
Return
}

Calculate old two-point distance and new two-point distance, zoom model, zoom model
float olddistance = vector2.distance (oldtouch1.position, oldtouch2.position);
float newdistance = vector2.distance (newtouch1.position, newtouch2.position);

Two distance difference, to indicate a magnification gesture, to reduce the gesture for negative representation
float offset = newdistance-olddistance;

Magnification factor, one pixel is calculated as 0.01 times times (100 adjustable)
float scalefactor = offset/100f;
Vector3 Localscale = Transform.localscale;
Vector3 scale = new Vector3 (localscale.x + scalefactor,
Localscale.y + Scalefactor,
Localscale.z + scalefactor);

Minimum zoom to 0.3 times times
if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f)
{
Transform.localscale = scale;
}

Remember the latest touch points, the next time you use
OldTouch1 = NewTouch1;
OLDTOUCH2 = NEWTOUCH2;
}
}

Unity releases Android finger to control object scaling and rotation

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.