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?