I have a character that has a constant speed, and I want to be able to change its direction with swipe gestures, The exact same mechanism implemented here.

I managed to do that for movement only, without taking into account the rotation, but I want to create this mechanism in such way that the rigidbody will have constant force in the transform.forward direction, and the Y rotation will change according to the swipe gestures, so the character will always move forward in local space, but will face towards different direction according to the assigned rotation.

I couldn't find a proper way of finding the desired rotation by the swipe direction.

How can I achieve this logic?

  • \$\begingroup\$ Maybe, you could try making a variable and adding the change in touch to it. Then you can take the x of the Vector2 and rotating the player based on that. A more detailed explanation will be provided if you request it. \$\endgroup\$
    – gbe
    Commented May 10, 2021 at 22:42
  • \$\begingroup\$ I actually did something similar but I need the character to be rotated by both x and y Touch.deltaPosition. That's make it harder to implement, unless I'm missing something \$\endgroup\$ Commented May 11, 2021 at 7:58

1 Answer 1


I found a solution using Quaternion.LookRotation() - I used it to translate the swipe gesture to a Vector3 that is representing the direction. I used this code inside the Update() method to retrieve the user's input:

            if (Input.touchCount > 0)
                currDirectionX = Input.touches[0].deltaPosition.x;
                currDirectionZ = Input.touches[0].deltaPosition.y;
            moveDirection = new Vector3(currDirectionX, 0f, currDirectionZ).normalized;

and this code inside the FixedUpdate() method to rotate towards the desired rotation, while constantly moving forward in local space:

    Quaternion targetRot = Quaternion.LookRotation(moveDirection);
    this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, targetRot, 8f);
    rb.AddForce((speed * transform.forward) - rb.velocity, ForceMode.VelocityChange);
  • \$\begingroup\$ Don't forget to mark this answer as Accepted if it worked for you. \$\endgroup\$
    – DMGregory
    Commented Feb 5, 2022 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .