Unity Mobile gesture operation-scaling 3D objects

Source: Internet
Author: User
Tags touch

A set of self-written for unity Mobile gesture operation of the judgment, mainly a single-finger moving 3D objects, single-finger rotation 3D objects, two-finger zoom 3D objects, here first separate two-finger zoom 3D object, as shown below:

Using Unityengine;

Using System.Collections;
    public class Zoomcontrol:gesturecontrol {//Record previous phone touch location to determine whether the user is in the left or zoom out gesture private Vector2 oldPosition1;
    Private Vector2 OldPosition2;
    Real-time size Vector3 Realscale = new Vector3 (1f, 1f, 1f);
    Original size float Initialscale = 0;
    Zoom speed public float scalespeed = 0.1f;
    Scale public float Maxscale = 2.5f;

    public float Minscale = 0.5f;
    void Start () {//Get object most primitive size Initialscale = this.transform.localscale.x; } protected override void Inputcheck () {#region Multi-touch scaling (true model scaling) if (input.touchcount ;
            1) {status = 2;
        Startcoroutine (Customonmousedown ()); } #endregion} IEnumerator Customonmousedown () {//When a constant touch is detected, the while (Input.get
            Mousebutton (0)) {//real-time record model size Realscale = This.transform.localScale; if (Input.gettouch (0). Phase = = touchphase.moved | | Input.gettouch (1). Phase = = touchphase.moved) {//Touch position Vector3 TempPosition1 =
                Input.gettouch (0). Position;

                Vector3 TempPosition2 = Input.gettouch (1). Position;
                function returns true for magnification, return false for narrowing if (Isenlarge (OldPosition1, OldPosition2, TempPosition1, TempPosition2)) 
                        {//Determine if the boundary is exceeded if (realscale.x < Initialscale * Maxscale) {
                    This.transform.localScale + = This.transform.localScale * scalespeed; }} else {//To determine whether to exceed the boundary if (realscal e.x > Initialscale * minscale) {This.transform.localScale-= This.transfor
                    M.localscale * SCALESPEED;
}}//Backup last touch position oldPosition1 = TempPosition1;                OldPosition2 = TempPosition2;
        } yield return new waitforfixedupdate (); }}//record finger position with initial position is reduced or enlarged bool Isenlarge (Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) {Floa
        T leng1 = mathf.sqrt ((op1.x-op2.x) * (op1.x-op2.x) + (OP1.Y-OP2.Y) * (OP1.Y-OP2.Y));
        float leng2 = mathf.sqrt ((np1.x-np2.x) * (np1.x-np2.x) + (NP1.Y-NP2.Y) * (NP1.Y-NP2.Y));
        if (Leng1 < leng2) {return true;
        } else {return false;
 }
    }
}


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.