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 Hiyek · Apr 12, 2018 at 10:44 PM · aiarray of gameobjectslinqpatrolnearest

Enemy nearest waypoint detector using LINQ, I need help updating the patrol array index using info from LINQ


Hello all!


Sorry if this seems trivial, but I've been coding all night. I just reached this issue on my list and my brain is running out of steam!


I currently have an enemy who patrols a top-down 2D space by going from waypoint to waypoint (using waypoint objects in an array, going from 0 then 1 then 2, etc)


         if (Vector3.Distance(transform.position, currentWP.position) < .1f)
         {
             if (currentWPIndex + 1 < waypoints.Length)
             {
                 currentWPIndex++;
             }
             else
             {
                 currentWPIndex = 0;
             }
             currentWP = waypoints[currentWPIndex];
         }



I also have a FindNearestWaypoint function that utilizes LINQ to locate the waypoint that is nearest to the enemy. This will get called when the enemy loses aggro, so it can follow its path from the nearest waypoint. Right now, when the enemy loses aggro, it attempts to go to the waypoint it was heading toward before aggro; meaning if it was at waypoint1 heading toward waypoint2 and I aggro it to chase me around all sorts of twists and turns across the map near waypoint20 and I lose aggro, it will attempt to walk in a straight line back toward waypoint2.


         GameObject FindNearestWaypoint() {
 
             GameObject wpOBJ;
             Vector3 position = transform.position;
             wpOBJ = GameObject.FindGameObjectsWithTag("Waypoint")
                 .OrderBy(o => (o.transform.position - position).sqrMagnitude)
                 .FirstOrDefault();
             Debug.Log(wpOBJ);
             //currentWP = wpOBJ.transform;
             //currentWPIndex = ?;
             return wpOBJ;
 
     }



Basically, what I want to do is figure out the corresponding index number for the returned wpOBJ and update the patrol index. The lines of commented out code were my attempts to take a crack at it, but I gotta be honest, I just don't have it in me! Once adding the currentWP = wpOBJ.transform; line, the enemy would go to the nearest waypoint after losing aggro as intended, but then it would just head to the previously stored index (using the example from the last section: starting at waypoint1, heading toward waypoint2, taking the enemy to waypoint20 then losing aggro ... the enemy would go to waypoint20, like intended, but rather than going to waypoint21 next, they would go back to waypoint2).


UPDATE: I tried manually making a converter which didn't work :/

 void ObjToIndex() {
 
         switch (wpOBJ.gameObject.name) {
 
             case "WP_1":
                 currentWPIndex = 0;
                 break;
 
             case "WP_2":
                 currentWPIndex = 1;
                 break;
 
             case "WP_3":
                 currentWPIndex = 2;
                 break;

etc


Also just tried this, but it didn't change anything.

 public int ObjToIndex() {
 
         switch (wpOBJ.gameObject.name) {
 
             case "WP_1":
                 currentWPIndex = 0;
                 return currentWPIndex;
                 
 
             case "WP_2":
                 currentWPIndex = 1;
                 return currentWPIndex;
 
             case "WP_3":
                 currentWPIndex = 2;
                 return currentWPIndex;


~ Thank you to anyone who can help me logic through this a bit! ~

Comment
Add comment · Show 1
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 Hiyek · Apr 12, 2018 at 10:55 PM 0
Share

P.S. I know that this isn't a proper pathfinding method (I plan on using A*) and that my solutions aren't the most efficient; I'm just rapidly iterating on a prototype for a better sense of game rules and mechanics before I get started on my actual project. Point being: quick and dirty solutions are absolutely welcome! :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hiyek · Apr 12, 2018 at 11:24 PM


I tried adding currentWPIndex = System.Array.IndexOf(waypoints,wpOBJ); to no avail.


Am I on the right track?

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

130 People are following this question.

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

Related Questions

How to remove z axis control from script 1 Answer

npc patrol and chase 1 Answer

Attack to enemy soldier 0 Answers

A* Path Finding (AI Destination Setter) Trying to set Target Transform to Nearest tagged Game Object (2D), 1 Answer

Enemy patrol is not working in AI Behavior plugin 0 Answers


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