# Change 2D GameObject based on touch direction

This is driving me crazy, how to change a 2D gameobject’s direction based on the touch swipe’s direction, without lefting the mouse up.

Just like in “Go Plane” game, the little plane in always moving forward in a constant speed, but the rotation is controlled by the user based on the swipe direction, wihtout the need to lift up his fingre.

I don’t want the plane to move toward the touch position, and i don’t want the player to touch in the upper-right corner of the screen in order for the plane to rotate to the upper-right direction.

What i want is to be able to capture the change in swipe direction (without lifting-up) in order to rotate and change the direction of the plane with a curve!

Any help is HIGHLY appreciated!

It’s not completely clear if you are asking about handling touch or mouse input.

In the case of touch, then touch delta position could be for you.

In the case of mouse, then take a look at getaxis

1 Like

Consider going through some tutorials, if you haven’t already done so: Learn

Believe me, I did! I always ended up with a behaviour different from what I am looking for. either the object following the mouse position, or you have to do a full circle swipe to rotate the object by 360 degrees.

here is my current code:

public static Vector2 PlayerDirection;

void Start () {
PlayerDirection = Vector2.zero;
currentPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
}

private Vector3 currentPos;

void Update () {
PlayerDirection = Camera.main.ScreenToWorldPoint (Input.mousePosition) - currentPos;
PlayerDirection = (PlayerDirection.magnitude > 1) ? PlayerDirection.normalized : PlayerDirection;
print (PlayerDirection);

float rotateAmount = Vector3.Cross (PlayerDirection, transform.up).z;

//Move player
transform.GetComponent<Rigidbody2D> ().angularVelocity = -rotateAmount * curveSpeed;
transform.GetComponent<Rigidbody2D> ().velocity = transform.up * flyingSpeed;
currentPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
}

I have tried Touch.DeltaPosition to calculate the swipe direction, but still it needs to swipe all the way uo the screen in order to change the game object rotation to face up!