# Move an object move from point a to point b to point c to point d and back to point a?

I need an object, specifically a spawn point to slowly move from point a,b,c, and d, back to point a continuously throughout the game. This is the code I have so far and beware it is longer than I think it ought to be. I really need help because I cannot find a way to do this. I am very new to this and don’t know the terms for a lot of functions, methods and variables. If anyone can help me I would be grateful. I am however going to still think of ways to do this. Also just in case you were wondering, I am purposefully trying to find ways out by myself to make myself better at this. Thanks in advance!

The problem that happens with this specific code is that the object only moves 0.2 places in the first direction and then stops. Other problems I have had with different ways of doing this is the object going straight into the centre of the 4 places i want it to go.

``````public class Spawn_Point_Movement : MonoBehaviour {

public Vector3[] currentPoint = new Vector3[4];
public float speed;
void Update () {
float step = speed * Time.deltaTime;

if (transform.position == currentPoint [0] && transform.position != currentPoint [1]
&& transform.position != currentPoint [2] && transform.position != currentPoint [3]
|| transform.position == Vector3.MoveTowards (transform.position, currentPoint [1], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [0], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [2], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [3], step)) {
transform.position = Vector3.MoveTowards (transform.position, currentPoint [1], step);
}

if (transform.position == currentPoint [1] && transform.position != currentPoint [2]
&& transform.position != currentPoint [0] && transform.position != currentPoint [3]
|| transform.position == Vector3.MoveTowards (transform.position, currentPoint [2], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [0], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [1], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [3], step)) {
transform.position = Vector3.MoveTowards (transform.position, currentPoint [2], step);
}

if (transform.position == currentPoint [2] && transform.position != currentPoint [3]
&& transform.position != currentPoint [1] && transform.position != currentPoint [0]
|| transform.position == Vector3.MoveTowards (transform.position, currentPoint [3], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [0], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [2], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [1], step)) {
transform.position = Vector3.MoveTowards (transform.position, currentPoint [3], step);
}

if (transform.position == currentPoint [3] && transform.position != currentPoint [0]
&& transform.position != currentPoint [2] && transform.position != currentPoint [1]
|| transform.position == Vector3.MoveTowards (transform.position, currentPoint [0], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [1], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [2], step)
&& transform.position != Vector3.MoveTowards (transform.position, currentPoint [3], step)) {
transform.position = Vector3.MoveTowards (transform.position, currentPoint [0], step);
}

}
}
``````

hex posted while i was typing. thought i would still answer. just name a bunch of game objects as “marker1”, “marker2”,“marker3” and so on. and put this script on your object you wanna move. this way you can have more than 4 if you wish.

``````	public int target;
public Vector3[] markers;

bool b;
int i;
void Start () {

//Put however many Empty game objects in your scene for position markers and name them "marker1" and "marker2" and "marker3"

b = false;
i = 0;
while (!b) {if (GameObject.Find ("marker" + i)) {i++;} else {b = true;}}
markers = new Vector3*;*
``````

_ while (i>0) {i–;markers = GameObject.Find (“marker”+i).transform.position;_

* } *

* }*

* void Update () {*
* transform.position = Vector3.MoveTowards (transform.position,markers[target], 2f);*
* if (transform.position == markers[target]) {target++;*
* if(target==markers.Length){target=0;}*
* }*

* }*

public Vector3 currentPoint = new Vector3[4];
int targetIndex = 0;
public float speed;
void Update () {
transform.position = Vector3.MoveTowards(transform.position, currentPoint[targetIndex], Time.deltaTime * speed);
if (transform.position == currentPoint[targetIndex])
targetIndex = (targetIndex + 1) % currentPoint.Length;
}