- Home /
How to make a gameObject follow a player
Hi, so I'm making a zombie survival game (Original, I know ;) ) and I want to make some AI, I thought an easy way for stopping the zombies from clipping through things and getting stuck would to be to follow my players footsteps, by dropping an invisible waypoint every half a second that a zombie follows. I would love someone to help me out with dropping waypoints and making a gameObject (Zombie) follow them, or link me to another post if this has already been answered, but I couldn't find anything :(
Thanks, Anthony.
Answer by revapps · Jul 21, 2014 at 06:21 AM
Add this to the zombie(s):
//You may consider adding a rigid body to the zombie for accurate physics simulation
private GameObject wayPoint;
private Vector3 wayPointPos;
//This will be the zombie's speed. Adjust as necessary.
private float speed = 6.0f;
void Start ()
{
//At the start of the game, the zombies will find the gameobject called wayPoint.
wayPoint = GameObject.Find("wayPoint");
}
void Update ()
{
wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
//Here, the zombie's will follow the waypoint.
transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
}
Add this to the player:
//In the editor, add your wayPoint gameobject to the script.
public GameObject wayPoint;
//This is how often your waypoint's position will update to the player's position
private float timer = 0.5f;
void Update ()
{
if(timer > 0)
{
timer -= Time.deltaTime;
}
if(timer <= 0)
{
//The position of the waypoint will update to the player's position
UpdatePosition();
timer = 0.5f;
}
}
void UpdatePosition()
{
//The wayPoint's position will now be the player's current position.
wayPoint.transform.position = transform.position;
}
Now, create an empty gameobject and make its position equal to the player's position, however, do NOT parent it. Name the empty gameobject "wayPoint". Within the editor, add the waypoint to the player. Be aware that this is a VERY basic script.
Thanks so much :D I really appreciate this. Is there a simple way of delete waypoints when the zombie gets to it, so if you play for an hour there aren't thousands of waypoints? $$anonymous$$aybe with colliders?
Again, thanks for the effort put into this ^-^
There's only one way point needed. Every 0.5 seconds, that same way point moves to the player's position.
Hi, Really nice... but i have one question. if there's an obstacles between zombie and the player means, how can the zombie cross it? for eg. player is standing in the hall, Zombie is in the room. the zombie want to find the entrance it want to come out on that way. it does not struck behind the wall. how can i do that? thanks in Advance
Change this line:
wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
to
wayPointPos = new Vector3(wayPoint.transform.position.x, wayPoint.transform.position.y, wayPoint.transform.position.z);
It was missing to follow the "wayPoint" at the Y axis.
Any way to add a code that makes it so it follows when the player is in the radius of the object?
Your answer
Follow this Question
Related Questions
How to play an animation with waypoints 1 Answer
Why isn't a Ai Waypoint Spawning??? 0 Answers
Moving previous waypoint with Lerp 1 Answer