How do I limit a character’s motion (Touch) to 3 specific points? Unity C#

Asked

Viewed 424 times

1

I would like to know how to limit the position of a character in the Y axis in the Up, Mid, Down positions. In the game the character runs while obstacles arise and dragging his finger up, if he is in the "down" position, he passes to the "Mid" position, and if he is in the "Mid" position, he can go to "up" (dragging up) or "down" (dragging down) and so on.I thank you in advance.

  • 1

    I suggest you ask the question, the code of your script so you can see where your difficulty was and where you have to change things. I hope I can help!

1 answer

2

You can use the function Vector3.MoveTowards. Then, within Update(), you can use an if statement to get the dragging finger. Dai you use Vector3.Movetowards to move your Object to the desired position. Sort of like this

 void Update() {
    if (Swipe pra cima) {
       float step = 3f * Time.deltaTime; //velocidade desejada de movimento.
       Vector3 posiçãoQueVoceQueira = new Vector3(transform.position.x, valorDeterminadoDeY, 0f);
       transform.position = Vector3.MoveTowards(transform.position, posiçãoQueVoceQueira, step);
    }
}

I hope it helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.