Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Ppa0 · May 21, 2011 at 12:29 AM · vector3aipathfindingwaypoint

Help with a simple AI

Hey guys I have made a simple waypoint function but I am having a bit of trouble. What I want it to do is this:

locate closest waypoint, go to it, if it reaches it, search again without including the one its currently on.

As you can see in the code below: (distanceToWayPoints[closest] < totalDistance) that block of code has to be changed

void WayPoint() {

// total distance

int totalDistance = 10;

 // goes through all the waypoints
 for (int nStart = 0; nStart < MaxNoOfWPs; nStart++)
 {
     // calculates all the distances from the monster to each waypoint
     distanceToWayPoints[nStart] = Vector3.Distance(everyWayPointInLevel[nStart].transform.position, transform.position);

     // if its the closest waypoint than have it be the target
     if (distanceToWayPoints[nStart] < distanceToWayPoints[closest])
     {
         closest = nStart;
         target = everyWayPointInLevel[closest].transform.position;

         //used as temp to know which one was the last wp
         lastWayPoint = everyWayPointInLevel[closest].transform.position;
     }

     // if the enemy gets close enough to the waypoint, trigger this = 
     if (distanceToWayPoints[closest] < totalDistance)
     {
         // THIS HAS TO BE CHANGED
         // so that instead of placing the wp at 0,0,0
         // it makes it not calculate the wp in the search
         everyWayPointInLevel[closest].transform.position = new Vector3(0,0,0);

     }
 }
 // animation
 animation.CrossFade("walk");
 // speed
 speed = walkingSpeed;

}

Comment
Add comment · Show 3
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 Owen-Reynolds · May 21, 2011 at 12:57 AM 0
Share

It seems like if it worked, it would just ping-pong between a small group of points. For example, if the waypoints were a rectangle, it would just walk back-and-forth on a short side.

avatar image Ppa0 · May 21, 2011 at 05:58 AM 0
Share

True, but I have about 70 wps all together scattered around the level, I will just have to see when this is fixed

avatar image Owen-Reynolds · May 21, 2011 at 04:26 PM 0
Share

You have the right idea (I think you can lose the distanceToWayPoints array, then add the currentWP idea, from below.) BUT, try this: put your finger on the starting WP. $$anonymous$$ove to the closest. Then move to the closest again (counting the starting point.) It will take about 30 seconds to do that, and I think you'll see the problem. Then pretend you add an "oldWP" var, to avoid going backwards, and hand trace that quickly. I think you'll always get some sort of loop.

2 Replies

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

Answer by DaveA · May 21, 2011 at 12:42 AM

I would set a new variable 'current' to the current waypoint's index, and have your loop avoid checking it if (current != nStart) or something like that.

Comment
Add comment · Show 1 · 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 Ppa0 · May 21, 2011 at 05:56 AM 0
Share

I think I have tried something like that already and it didn't work but I will try it again, maybe I was doing it wrong somehow.

avatar image
0

Answer by Ppa0 · May 21, 2011 at 09:29 PM

I had to tweak it a bit, but adding the variable helped out a lot, Thank you both!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Incredibly high speed tweening/enemy movement 0 Answers

Enemy detection while pathfinding through the map 1 Answer

AI Pathfinding 1 Answer

Why isn't a Ai Waypoint Spawning??? 0 Answers

AI Pathfinding In A Certain Height 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