- Home /
Want to make GameObject follow another GameObject's path Unity3D
Hello, I am trying to make a GameObject follow the path exactly to a player GameObject with a delay. I have looked everywhere and most scripts make the follower Lerp to the current position of the player which means it takes the shortest path rather than follow the path that the player took. So if I go around in a circle, the follower is inside the circle spinning around.
Here is the code that I am currently using:
void Update()
{
transform.position = Vector3.Lerp(transform.position, target.position, speedPosition * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, speedRotation * Time.deltaTime);
}
Visual of what is happening:
(Red is player, Pink is path took by player;Blue is follower, light blue is path took by follower)
I want the blue path to match the pink path but be slightly behind with a delay
I plan on having a chain of followers following the same path if that helps/complicates anything
Any help is appreciated!
Answer by Tsaras · Mar 07, 2019 at 06:32 AM
You can store the position of player in an array and have the others follow that. Maybe not every frame, something around 100ms per positoin could be ok.
That is my second option, Im using TrailRenderer and using GetPositions into an list. Getting some errors but will see if thats the better option.
Thank you!