Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AnthonySturdy · Jul 17, 2014 at 09:16 PM · waypointwaypoint system

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AnthonySturdy · Jul 21, 2014 at 11:02 AM 0
Share

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 ^-^

avatar image revapps · Jul 21, 2014 at 12:43 PM 0
Share

There's only one way point needed. Every 0.5 seconds, that same way point moves to the player's position.

avatar image Manoj Balaji Mohan · Jul 01, 2016 at 06:50 AM 0
Share

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

avatar image IshanCK · Jun 24, 2017 at 06:19 PM 0
Share

um when i use this script the enemy goes left and right

avatar image PredOborG IshanCK · Jul 12, 2018 at 06:42 PM 0
Share

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.

avatar image unity_143462 · Feb 19, 2020 at 02:46 PM 0
Share

Any way to add a code that makes it so it follows when the player is in the radius of the object?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How will i make my objects/model(say a car) face the way points and travel towards it,instead of blindly following it? 2 Answers

How do I make a waypoint loop function using array? - when it gets to a waypoint, then move to next waypoint 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges