MoveTowards cant be stopped

Viewed 57

I have been making game in which you can go upstairs and when you press w MoveTowards will move you in the middle of stairs. Everything is working perfectly but i cant stop MoveTowards when animation stops and player is in middle of stairs. But MoveTowards still pulls me inside and player can´t move.

This is code that pulls player:

if(entered == 1 && klikw == 1)
{
     col.SetActive(true);
     Vector3 a = transform.position;
     Vector3 b = location.position;
     transform.position = Vector3.MoveTowards(a, b, speed);
     animator.SetFloat("whenhewalk", 1);
}

klikw is when player clicks w

1 Answers
  • List item

float minDistance = 1.2f;

 void Update ()
 {
     if (Vector2.Distance(transform.position, Player.transform.position) > minDistance ){
     transform.position = Vector2.MoveTowards(transform.position, Player.transform.position, step);
     }
 }
Related