Using touch controls in EasyTouch

1 import this package into the unit scenario


Import package 1

Import package 2

2 uncheck unnecessary documents


Uncheck

In your core control mobile script, first add the import statement as follows
using HedgehogTeam.EasyTouch;

Then add the following code to the control statements

Gesture currentGesture = EasyTouch.current;
if(currentGesture!=null&&currentGesture.swipe==EasyTouch.SwipeDirection.Up)

If your original logic is as follows

void FixedUpdate () {
        Vector2 p=Vector2.MoveTowards (transform.position,dest,speed);
        gameObject.GetComponent<Rigidbody2D> ().MovePosition (p);
        Gesture currentGesture = EasyTouch.current;
        if ((Vector2)transform.position == dest) {
            if ((Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W))&&Valid(Vector2.up)) {
                dest = (Vector2)transform.position + Vector2.up;
            }
            if ((Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))&&Valid(Vector2.right)) {
                dest = (Vector2)transform.position + Vector2.right;
            }
            if ((Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S))&&Valid(Vector2.down)) {
                dest = (Vector2)transform.position + Vector2.down;
            }
            if ((Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))&&Valid(Vector2.left)) {
                dest = (Vector2)transform.position + Vector2.left;
            }
        }
        Vector2 dir = dest - (Vector2)transform.position;
        gameObject.GetComponent<Animator> ().SetFloat ("DirX",dir.x);
        gameObject.GetComponent<Animator> ().SetFloat ("DirY",dir.y);
    }

Now it's going to be like this

void FixedUpdate () {
        Vector2 p=Vector2.MoveTowards (transform.position,dest,speed);
        gameObject.GetComponent<Rigidbody2D> ().MovePosition (p);
        Gesture currentGesture = EasyTouch.current;
        if ((Vector2)transform.position == dest) {
            if (((currentGesture!=null&&currentGesture.swipe==EasyTouch.SwipeDirection.Up)||(Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W)))&&Valid(Vector2.up)) {
                dest = (Vector2)transform.position + Vector2.up;
            }
            if (((currentGesture!=null&&currentGesture.swipe==EasyTouch.SwipeDirection.Right)||(Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)))&&Valid(Vector2.right)) {
                dest = (Vector2)transform.position + Vector2.right;
            }
            if (((currentGesture!=null&&currentGesture.swipe==EasyTouch.SwipeDirection.Down)||(Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S)))&&Valid(Vector2.down)) {
                dest = (Vector2)transform.position + Vector2.down;
            }
            if (((currentGesture!=null&&currentGesture.swipe==EasyTouch.SwipeDirection.Left)||(Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)))&&Valid(Vector2.left)) {
                dest = (Vector2)transform.position + Vector2.left;
            }
        }
        Vector2 dir = dest - (Vector2)transform.position;
        gameObject.GetComponent<Animator> ().SetFloat ("DirX",dir.x);
        gameObject.GetComponent<Animator> ().SetFloat ("DirY",dir.y);
    }

Is to add the condition of sliding event, do you understand?

Tags: Mobile

Posted on Tue, 05 May 2020 13:54:07 -0700 by surajkandukuri