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 wolmer · Jan 13, 2016 at 10:27 AM · navmeshnavmeshagentdistancedistance check

How to get a list of the closest distance targets with NavMesh.

So we have an enemy, which is supposed to target the closest target available (the shortest path distance). But somehow this code only returns the distance of one path. Please help :)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class AI : MonoBehaviour
 {
     //Public variables for testing purpose
     public List<GameObject> enemies = new List<GameObject>();
     public List<GameObject> sortedEnemies = new List<GameObject>();
     public List<float> distances = new List<float>();
     public List<float> sortedDistances = new List<float>();
 
     protected GameObject target;
     private NavMeshPath myPath = new NavMeshPath();
 
     // Public for the sake of seeing it in Inspector
     public float statPool = 10f;
     public float movementSpeed;
     public float enrageDistance = 2f;
     private bool enrage = false;
 
     public float attackSpeed; // Seconds before hit. aka 2 = 0.5 hits per second
     public float attackCountdown;
 
     void Start()
     {
         // Needs some kind of fixing, but I can't think of a smart one.
         NavMeshAgent agent = GetComponent<NavMeshAgent>();
         // rolling out random stats for the snail
         // gonna need a static int with amount of "rounds"
         statPool -= 3f; // This is to compensate for the minimum of 1 in each stat
         //health = Random.Range(1, statPool); // statpool * "rounds" and prob divided by something
         // damage = Random.Range(1, (statPool - health)); // -ll-
         movementSpeed = 1 + (statPool /*- health - damage*/); // -ll-
         agent.speed = movementSpeed;
 
     }
 
     void Update()
     {
         if (enrage == false)
         {
             Aim();
         }
         else
         {
             GetComponent<NavMeshAgent>().SetDestination(target.transform.position);
         }
         if (attackCountdown > 0)
         {
             attackCountdown -= Time.deltaTime;
         }
     }
 
     void Aim()
     {
         //Clear old data.
         enemies.Clear();
         distances.Clear();
         sortedEnemies.Clear();
         sortedDistances.Clear();
         target = null;
 
         //Check for taunt
         if (GameObject.FindGameObjectsWithTag("Taunt").Length > 0)
             enemies = new List<GameObject>(GameObject.FindGameObjectsWithTag("Taunt"));
         else
             enemies = new List<GameObject>(GameObject.FindGameObjectsWithTag("PlayerRelated"));
 
         //Set distance between every enemy and this unit.
         for (int i = 0; i < enemies.Count; i++)
         {
             target = enemies[i];
             GetComponent<NavMeshAgent>().SetDestination(target.transform.position);
             Debug.Log("Distance between AI and " + target.gameObject.name + " is " + GetComponent<NavMeshAgent>().remainingDistance);
             distances.Add(GetComponent<NavMeshAgent>().remainingDistance);
         }
         Sort(distances, enemies);
         target = sortedEnemies[0];
 
         if (target == null)
             return;
 
         GetComponent<NavMeshAgent>().SetDestination(target.transform.position);
         //if (GetComponent<NavMeshAgent>().remainingDistance <= enrageDistance && target != null)
         //    enrage = true;
 
     }
 
     void Sort(List<float> floatList, List<GameObject> objectList)
     {
         float tempFloat;
         GameObject tempObject;
         bool sorted = false;
 
         while (sorted == false)
         {
             sorted = true;
             for (int i = 0; i < objectList.Count - 1; i++)
             {
                 //Switch.
                 if (floatList[i + 1] < floatList[i])
                 {
                     tempFloat = floatList[i + 1];
                     tempObject = objectList[i + 1];
                     floatList[i + 1] = floatList[i];
                     objectList[i + 1] = objectList[i];
                     floatList[i] = tempFloat;
                     objectList[i] = tempObject;
 
                     sorted = false;
                 }
             }
         }
         sortedDistances = floatList;
         sortedEnemies = objectList;
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

Navmesh agent remaining distance wrong when movement done by root motion of Mecanim? 1 Answer

remaining distance strange bug 0 Answers

Why is my list bigger than it should be? 2 Answers

How to fix indecisive Navmesh Agent? 0 Answers

how to search for a random point until the condition is true? 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