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);
}
}
}
Answer by toddisarockstar · Feb 16, 2017 at 01:12 AM
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[i];
while (i>0) {i--;markers[i] = 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;}
}
}
Answer by hexagonius · Feb 15, 2017 at 05:16 PM
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;
}
Thank you so much! This worked perfectly and I learnt something new! :)
the marker object which is currentPoint1, currrentPoint2 and so on??
Your answer
Follow this Question
Related Questions
Help me to do this moving player script 0 Answers
Script problems c# 0 Answers
Rigidbody2D movement is lagging 0 Answers
How do I make my movement script move the character UNTIL I let go of the key? 1 Answer
Restarting after pressing a key 0 Answers