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 Velketor · Mar 09, 2011 at 03:28 PM · aienemypathwaypoints

Waypoint Setup: How?

Can anyone please explain how to set up a proper waypoint system? I want my enemy to patrol along a given waypoint path and if I get within range of the enemy it will attack me. If I run out of range of the enemy, it should go back to it's patrol, or at least the nearest waypoint.

I've been playing with example projects and scripts from all over the place and still no luck. Any help is greatly appreciated. Thank you!

Shawn

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by FizzyBear · Apr 15, 2012 at 11:21 PM

Create an empty gameobject, name it waypoint, duplicate it, drag it and make it a child of the waypoint. Position them where ever. Ta da. You got your waypoint.

For the code however.. I'd do something like this..

   // speed of the AI player
 public var speed:int = 4;
  
 // speed the ai player rotates by
 public var rotationSpeed:int = 5;
  
 // the waypoints
 public var waypoints:Transform[];
  
 // current waypoint id
 private var waypointId:int = 0;
  
 /**
     Patrol around the waypoints
 */
 function Patrol()
 {
     // if no waypoints have been assigned
     if (waypoints.Length == 0) 
     {
         print("You need to assign some waypoints within the Inspector");
         return;
     }
      
     // if distance to waypoint is less than 2 metres then start heading toward next waypoint
     if (Vector3.Distance(waypoints[waypointId].position, transform.position) < 2)
     {
         // increase waypoint id
         waypointId++;
          
         // make sure new waypointId isn't greater than number of waypoints
         // if it is then set waypointId to 0 to head towards first waypoint again
         if (waypointId >= waypoints.Length) waypointId = 0;
     }
      
     // move towards the current waypointId's position
     MoveTowards(waypoints[waypointId].position);
 }
  
  
 /**
     Update - Every Frame
 */
 function Update()
 {
     // Patrol!
     Patrol();
 }

Now you got your guy going from 1 waypoint to the other..

Comment
Add comment · Show 2 · 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 Estevominador · Jun 03, 2012 at 05:57 PM 0
Share

It doesnt work! it says: "Assets/Scripts/Waypoints.js(38,5): BCE0005: $$anonymous$$ identifier: '$$anonymous$$oveTowards'."

avatar image chiako · Feb 24, 2013 at 11:31 AM 0
Share

where should I attach this script? and... is the gameobject that you mentioned is the enemy? or just the waypoint?

avatar image
0

Answer by Tuck · Mar 09, 2011 at 07:38 PM

Do you want the enemy to chase after the player when the player is in range or just stop moving along the path and start shooting? Implementing path following AI is easy. Having enemy AI chase the player intelligently is less so.

Update: The simplest way I've found to make a path following AI requires three parts: an enemy object, a parent path object, and individual path node objects.

The enemy has a public game object that you can assign in the editor to the path object you want it to follow, and a function that moves it towards a given position. In the update function of the enemy have it move toward the position of a node in the list, and upon reaching it have it move to the next node.

The parent path object needs a public list that will contain all nodes in the path and a function that sorts the list based on an int inside of the node class.

The node object needs a public int (as mentioned above) that determines its order in the path. It also needs a function that adds it to the parent object's list of nodes to be called on start.

I realize that's a complicated explanation. I'm tempted to just make a sample project and upload it. I dunno, let me know how it goes.

Comment
Add comment · Show 2 · 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 Velketor · Mar 09, 2011 at 11:13 PM 0
Share

Lets just go with AI moving from waypoint to waypoint. I don't even know how to create a waypoint. What scripts are involved? Please help. I appreciate it! Thank you for your repsonse.

$$anonymous$$

avatar image Tuck · Mar 10, 2011 at 07:28 PM 0
Share

Alright I updated my post with the logic for a path following system. A waypoint in this case is just a cube object or something with a script attached.

avatar image
0

Answer by Dee Va · Jun 18, 2012 at 01:55 PM

If it says: "Assets/Scripts/Waypoints.js(38,5): BCE0005: Unknown identifier: 'MoveTowards'."

add this to your script

 function MoveTowards (position : Vector3) {
     var direction = position - transform.position;
     direction.y = 0;
     if (direction.magnitude < 0.5) {
         SendMessage("SetSpeed", 0.0);
         return;
 }
 
 }
 hope it works :)
Comment
Add comment · 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
0

Answer by filip0258 · Mar 12, 2014 at 10:18 AM

function MoveTowards (position : Vector3) { var direction = position - transform.position; direction.y = 0; if (direction.magnitude < 0.5) { SendMessage("SetSpeed", 0.0); return; }

} hope it works :)

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

2D enemy Field of Vision script 1 Answer

Need Help With My AI Script 1 Answer

AI script troubles 1 Answer

Enemy AI things to consider 1 Answer

How do I animate my enemy randomly with time? 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