Home > Enterprise >  Vector2.MoveTowards not stopping at target position
Vector2.MoveTowards not stopping at target position

Time:06-05

I am trying to move an object from its current position (on a trigger call) to a target position at a specific speed and make it stop at the target.

This is the code I have:

bool needsToMove = false; 
float speed = 5f;


void OnTriggerEnter2D(Collider2D collision)
{
    needsToMove = true;
}

void FixedUpdate()
{
    MoveDown();
}


void MoveDown()
{
    if (needsToMove)
    {
        Vector2 targetPosition = new Vector2(transform.position.x, transform.position.y - 1f);
        transform.position = Vector2.MoveTowards(transform.position, targetPosition, Speed * Time.deltaTime);
      
    }
    
}

The problem is, that the game object doesn't stop at the target position, it keeps going in the same direction.

I tried adding a coroutine to switch the bool "needsToMove" to false after a couple of seconds but doing that is tedious as I have to pretty much guess the exact time it will take to stop at the target.

Maybe I need to use another method instead of MoveTowards?

CodePudding user response:

This is because your target path depends on the current location, from where the target axis is taken from transform.position it acts like a fox tail and the object is always finding it. There are many ways to solve this problem such as IEnumerator or using Tweener. But there is a simple way to define a destination-oriented function.

public float stopDistance = .2f;
public Vector2 destination;

public void Move()
{
    if (Vector2.Distance(transform.position, destination) <= stopDistance) return;

    transform.position = Vector2.MoveTowards(transform.position, destination, Speed * Time.deltaTime);
}
public void FixedUpdate()
{
    Move();
}

In the above code you just need to enter the destination when a specific event such as OnTriggerEnter, the object stops moving when the distance is below the stopDistance value. To call the Move, set the destination in as below.

void OnTriggerEnter2D(Collider2D collision)
{
    destination = new Vector2(transform.position.x, transform.position.y - 1f);
}

CodePudding user response:

Thanks everyone for making me realize the position was being constantly changed.

What I did to fix was to move the destination/target code to Start()that way the position gets saved and is not being constantly changed on Update()

bool needsToMove = false;
float speed = 5f;
//I added this variable here
Vector2 targetPosition; 


void Start()
{
  //I defined the target position on Start so it is not being constantly changed by Update()
  targetPosition = new Vector2(transform.position.x, transform.position.y - 1f);
}

void OnTriggerEnter2D(Collider2D collision)
{
    needsToMove = true;
}

void FixedUpdate()
{
    MoveDown();
}


void MoveDown()
{
    if (needsToMove)
    {
       //I moved this line to Start(): Vector2 targetPosition = new Vector2(transform.position.x, transform.position.y - 1f);
        transform.position = Vector2.MoveTowards(transform.position, targetPosition, Speed * Time.deltaTime);
      
    }
    
}
  • Related