# Moving from point A to point B to point A to point B over and over

Basically I’m wanting a enemy to move back and forth from two points that I’ve assigned called point A and point B.

I’ve looked up over topics about this similar thing and none of them seemed to help so I thought I would create a new topic. The main thing is I’m using a enum to switch between the different states for the enemy and one of them is a nonAggro state which is when I want the enemy to move back and forth from point A and point B.

I also want to keep this PingPong motion going where the enemy moves up and down slightly, when it moves from point A to B and back to A as it doesn’t when using things like lerp.
Here is the PingPong script:

``````if(canMove)
{
transform.position = new Vector3(transform.position.x, yCenter + Mathf.PingPong(Time.time, 0.5f), transform.position.z);
}
``````

Could anyone help?

You’re on the right track, but there’s a much more configurable way of implementing that:

``````public Vector3 pointA;
public Vector3 pointB;
public float repeatTime = 0.5f;

// ...

if(canMove && repeatTime > 0)
{
transform.position = Vector3.Lerp(pointA, pointB, Mathf.PingPong(Time.time, repeatTime) / repeatTime)
}
``````

Using Linear Interpolation to go back and forth between points A and B (defined freely), The PingPong value can be scaled into a 0-1 range by dividing by its own upper limit.