Hey guys, I am pretty new to scripting…
I am trying to make an object to go from pointA to pointB to pointC.
So far the object is going ok from pointA to PointB, but when it reaches pointB it immediately snaps on pointC. I dont know why?
I’ve been searching the forum and I find people usually do this with arrays, but my point is that I want to try write my own script rather than copy-paste for the sake of learning. 
Thanks in Advance!
var pointA : Transform;
var pointB : Transform;
var pointC : Transform;
var speed : int = 10;
function Update () {
transform.position = Vector3.MoveTowards(pointA.position, pointB.position, Time.time * speed);
if (transform.position == pointB.position) {
transform.position = Vector3.MoveTowards(pointB.position, pointC.position, Time.time * speed);
}
}
Things to keep in mind:
- Update is called every frame (30+ times a second). It’s run from the beginning of the function every time.
- Time.time starts counting up from when the game starts, and never resets to zero.
- transform.position will only equal pointB.position at the moment it hits that position, not for every time after that.
So with that in mind, here’s what your code is doing, every frame:
- Move from A toward B, a distance equal to 10 units per second the game has been running. It still does this even after you reached B.
- If that’s enough to reach B, go from B toward C a distance equal to 10 seconds per unit the game has been running. Which means it immediately reaches B if B-C is shorter than A-B
What you need to do is keep track of your current state. You need to track two things:
- What phase of movement you’re in (moving from A to B, moving from B to C, finished moving)
- How long it’s been since you started your current phase of movement
private enum State { A_TO_B, B_TO_C, DONE };
private var currentState : State = State.A_TO_B;
private var currentTime : float = 0;
In Update, you need to determine what you do by what state you’re in:
- In State.A_TO_B, add Time.deltaTime to the currentTime, and moving from A to B by the currentTime.
- In that state, once you reach B, reset the currentTime to 0, and switch to State.B_TO_C
- In State.B_TO_C, do the same thing as A_TO_B, except B to C instead
- In that state, once you reach C, switch to State.DONE so you won’t waste any more time moving it
Code, if needed:
function Update () {
if(currentState == State.A_TO_B) {
currentTime += Time.deltaTime;
transform.position = Vector3.MoveTowards(pointA.position, pointB.position, currentTime * speed);
if(transform.position == pointB.position) {
currentTime = 0;
currentState = State.B_TO_C;
}
}
else if(currentState == State.B_TO_C) {
currentTime += Time.deltaTime;
transform.position = Vector3.MoveTowards(pointB.position, pointC.position, currentTime * speed);
if(transform.position == pointC.position) {
currentState = State.DONE;
}
}
}
Further improvements you could make:
- Have it support a list of points (Transform[ ]) instead of always being three. For this purpose, you may want to change currentState to an int index.
- Instead of automatically starting, have a StartMoving function, which could be triggered by a button or specified delay.
ErrorSatz, thank you very much for taking the time to explain in such detail. 
I unsderstand the idea, but I am not familiar with “enum” and when I try to compile this, the compiler spits back this:
BCE0005: Unknown identifier: ‘B_TO_C’.
BCE0005: Unknown identifier: ‘DONE’.
Since we say that currentState : State = State.A_TO_B;
Somehow it doesn’t recognize the other, How can I fix that? 
Thank you!
Actually, sorry about that, Everything is OK. I just made some stupid mistakes in my syntax.
It works sweeeeet 
Thanks again!!