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 /
  • Help Room /
avatar image
0
Question by noaROX · Mar 25, 2020 at 10:05 AM · array

Finding the shortest path between nodes not working for enemy AI

My code is designed to make the enemy navigate between checkpoints which are manually assigned using the shortest path it can find. It works for the first 2 checkpoints but then gets 'stuck' at the second checkpoint. Can you see what my issue is?

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class enShort3 : MonoBehaviour {
     //this code will find the closest checkpoint to the enemy and move them towards it repeatedly
     
     public GameObject[] verts; //array of checkpoints for enemy to move between
     
     Transform objective; //transform for where enemy is aiming to go
     int shortex = 0; //index for checkpoint with shortest path
     Transform spawn; //ransform for enemies initial spawn
     float shortest = 0; //record of shortest path found
     public float speed = 10f; //speed mutliplier for enemy movement
 
     private void Start()
     {
         spawn = this.transform; //records location at spawn
         shortest = Vector3.Distance(spawn.transform.position, verts[0].transform.position); //default quickest path is between enemy's spawn and first
                                                                                             //checkpoint index
         shortex = 0; //index of first checkpoint
     }
 
     private void FixedUpdate()
     {
         objective = verts[shortex].transform; //enemy's objective is defined by the index of the shortest path found in the array
         this.transform.position += transform.forward * Time.deltaTime * speed; //move the enemy forward constantly
         this.transform.LookAt(objective); //Rotate the enemy towards the objective
 
         if (Vector3.Distance(this.transform.position, objective.position) < 5) //if the enemy is close enough to its current objective...
         {
             for (int x = 0; x < verts.Length; x++) //for every array value...
             {
                 if (x < verts.Length - 3) //if the current index is not 2 below the end of the array... 
                 {
                     if (Vector3.Distance(verts[x].transform.position, verts[x + 1].transform.position) <
                         Vector3.Distance(verts[x].transform.position, verts[x + 2].transform.position)) //check if the distance to the second array index
                                                                                            //is shorter than the distance to the third one as the enemy is
                                                                                            //already at the first index
                     {
                         float temp = Vector3.Distance(verts[x].transform.position, verts[x + 1].transform.position); //record the shorter distance
 
                         if (temp <= shortest) //check if the shorter distance is shorter than the shortest found so far
                         {
                             shortest = temp; //if it is, update the shortest distance
                             shortex = x + 1; //set the index to point towards the shortest distance
                             
                         }
                     }
 
                     else if (Vector3.Distance(verts[x].transform.position, verts[x + 1].transform.position) > //repeat the same here
                             Vector3.Distance(verts[x].transform.position, verts[x + 2].transform.position))
                     {
                         float temp = Vector3.Distance(spawn.transform.position, verts[x + 2].transform.position);
                         if (temp < shortest)
                         {
                             shortest = temp;
                             shortex = x + 2;
                             
                         }
                     }
 
                 }
 
                 objective = verts[shortex].transform; //set the objective using the closest checkpoint found
             }
 
         }
 
     }
 
 }

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
0

Answer by streeetwalker · Mar 25, 2020 at 05:29 PM

@noaROX, I'm trying to understand how your code fits with your purpose.


You want your enemy to move from checkpoint 1, to, 2, and then to 3? Or, you want it to move to a checkpoint and then choose the next closest checkpoint... or what?

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

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

Can I instantiate objects from an array 1 at a time? 1 Answer

Compare each vector3 from an array to each vector3 in a list and add objects which do not exist into the list 0 Answers

how to check the bool of multiple object in an arrya. 0 Answers

Checking if a point is withing any of the colliders 1 Answer

Array 2D of data 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