I have one problem with Vector3.Lerp.
I have, in the scene, 3 spawning points and one exit point.
I instantiate a sphere randomly in one of this 3 spawning points every 3 seconds. With Vector3.Lerp the sphere generated goes to exitPoint.
the first sphere generated works well.
Here below the script attached to the sphere prefab.:
float speed = Mathf.Repeat(Time.time / 10.0f, 1.0f);
transform.position = Vector3.Lerp (spawnPosition.position, targetPosition.position, speed);
But the second sphere generated don’t start to lerp from spawn position, but start at the current position of the first sphere generated, idem the third ecc. ecc.
How can I fix it?
Every sphere must start from spawning point where is generated, with the same script attached.
Sorry for my English, I’m trying to learn it by myself.
The last parameter (t) in
Lerp should range between 0 and 1. When it’s 0 it returns the first position, when it’s 1 it returns the last position, and when it’s between 0 and 1 it returns a position in between.
t can be considered a percentage - as in “give me a position that’s t% between the first and second position”. So 0.5 gives you a position halfway between, 0.25 is 25% between, and so on.
With that in mind, how does your
speed variable act as a percentage? The call to
Repeat makes it loop between 0 and 1, which is good. However when you spawn a new entity it’s going to start wherever
speed happens to be in at that time.
So instead of using
Time.time, create a float variable that starts at 0 when the object spawns, and in update add
Time.deltaTime to it each frame. Plug that into your
Mathf.Repeat function. This way each object gets its own timer that starts at 0 when the object is spawned.