1
\$\begingroup\$

I have some code attached to a moving platform:

public Transform pos1, pos2;
public float speed;
public Transform startPos;

Vector3 nextPos;
// Start is called before the first frame update
void Start()
{
    nextPos = startPos.position;
}

// Update is called once per frame
void Update()
{
    if (transform.position == pos1.position)
    {
        nextPos = pos2.position;
    }
    else if (transform.position == pos2.position)
    {
        nextPos = pos1.position;
    }
    transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}

private void OnDrawGizmos()
{
    Gizmos.DrawLine(pos1.position, pos2.position);
}

But my platform does not go to the pos2 when it reaches the pos1. What is wrong here?

\$\endgroup\$
1
  • \$\begingroup\$ When do you tell your platform to move toward pos1? All I see is it moving toward startPos. If startPos does not coincide with pos1, then your platdorm never moves to the position that the first if condition is checking for \$\endgroup\$
    – DMGregory
    Commented Oct 25, 2019 at 13:27

3 Answers 3

1
\$\begingroup\$

I'm not sure how "MoveTowards" method works, but if you're working on floats then most likely you'll never have exact match of positions.

When doing path followers, I always check the distance between current position and target (pseudocode):

if (VectorLength(transform.position-pos1.position) < speed*Time.deltaTime)
{
    nextPos = pos2.position;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Unity already takes floating point precision into account when comparing vectors, and also does what your code snippet does in MoveTowards. \$\endgroup\$
    – Ed Marty
    Commented Oct 23, 2019 at 13:48
0
\$\begingroup\$

I agree with @crueltear's answer, I just wanted to provide some code that you might add in your script:

Since it is likely that the transform.position pos1.position will not perfectly match, try and add a bit of "discrepancy" so that instead of waiting for the positions to match perfectly you just have to wait for them to be within a certain distance.

Try and introduce a float, e.g. public float gapDistance and replace in your if statements

(transform.position == pos1.position)

With

(Vector3.Distance(transform.position , pos1.position) <= gapDistance)

Do the same for pos2.position. If you are using 2D Vectors instead of 3D, just change Vector3 with Vector2

\$\endgroup\$
0
\$\begingroup\$

I would recommend adding a few debug logs in update to see what it’s doing.

// Update is called once per frame
void Update()
{
    if (transform.position == pos1.position)
    {
        nextPos = pos2.position;
        Debug.Log("Starting move to pos2 at " +  nextPos);
    }
    else if (transform.position == pos2.position)
    {
        nextPos = pos1.position;
        Debug.Log("Starting move to pos1 at " +  nextPos);
    }
    transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    Debug.Log("move towards " + nextPos + "; new position is " + transform.position);
}

Check the console to see what is getting logged.

\$\endgroup\$

You must log in to answer this question.

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