Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Nov 12, 2020 at 03:13 PM by kuba86699 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kuba86699 · Nov 11, 2020 at 01:22 PM · aitagtargetdistance check

How to make the bot not check the distance from itself to itself?

All AI bots using this script have the same tag. How to make the bot not check the distance from itself to itself?

 using System.Collections;
  using System.Collections.Generic;
  using System.Data;
  using UnityEditorInternal;
  using UnityEngine;
  using UnityEngine.AI;
  using UnityEngine.SceneManagement;
  
  public class NewBehaviourScript : MonoBehaviour
  {
  
      NavMeshAgent _navMeshAgent;
  
      private GameObject player;
      private GameObject ai;
  
      private float aiPlayerDist;
      private float aiAiDist;
  
      void Start()
      {
          _navMeshAgent = this.GetComponent<NavMeshAgent>();
  
          if(_navMeshAgent == null)
          {
              Debug.LogError("ERROR" + gameObject.name);
          }
      }
  
      void Update()
      {
          SetDestination();
      }
  
      private void SetDestination()
      {
          player = GameObject.FindGameObjectWithTag("Player");
          ai = GameObject.FindGameObjectWithTag("Ai");
  
          aiPlayerDist = Vector3.Distance(player.transform.position, transform.position);
          aiAiDist = Vector3.Distance(ai.transform.position, transform.position);
  
          if (player != null || ai != null)
          {
              if (aiPlayerDist > aiAiDist)
              {
                 Vector3 targetVector = ai.transform.position;
                 _navMeshAgent.SetDestination(targetVector);
              }
              else if (aiPlayerDist < aiAiDist)
              {
                  Vector3 targetVector = player.transform.position;
                  _navMeshAgent.SetDestination(targetVector);
              }
          }
  
          Debug.Log(aiAiDist);
  
      }
  }
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 $$anonymous$$ · Nov 11, 2020 at 02:30 PM 1
Share

When you're getting the AI object, using GameObject.FindGameObjectWithTag, the script may be getting the same object it exists on.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by andrew-lukasik · Nov 11, 2020 at 03:10 PM

Do it this way:

 foreach( var nextComponent in SomeComponentType.Instances )
 {
     if( nextComponent!=this )
     {
         /* not me */
     }
 }

More on how to create that SomeComponentType.Instances in code below.


Enemy.cs:

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 [DisallowMultipleComponent]
 [RequireComponent( typeof(NavMeshAgent) )]
 public class Enemy : MonoBehaviour
 {
     public static List<Enemy> Instances = new List<Enemy>(1);
     [SerializeField] NavMeshAgent _navMeshAgent;// just fill manually and be done with it
     const float _aiTickRate = 0.1f;// [seconds]
     void OnEnable ()
     {
         Instances.Add( this );
         InvokeRepeating( nameof(AiTick) , _aiTickRate , _aiTickRate );
     }
     void OnDisable ()
     {
         Instances.Remove( this );
         CancelInvoke( nameof(AiTick) );
     }
     void AiTick ()
     {
         Vector3 myPosition = transform.position;
 
         // find nearest player:
         Player nearestPlayer = null;
         float nearestPlayerDistance = float.MaxValue;
         Vector3 nearestPlayerPosition = Vector3.zero;
         foreach( var nextPlayer in Player.Instances )
         {
             Vector3 nextPos = nextPlayer.transform.position;
             float nextDist = Vector3.Distance( myPosition , nextPos );
             if( nextDist<nearestPlayerDistance )
             {
                 nearestPlayer = nextPlayer;
                 nearestPlayerDistance = nextDist;
                 nearestPlayerPosition = nextPos;
             }
         }
 
         // find nearest enemy:
         Enemy nearestOtherEnemy = null;
         float nearestOtherEnemyDistance = float.MaxValue;
         Vector3 nearestOtherEnemyPosition = Vector3.zero;
         foreach( var nextEnemy in Enemy.Instances )
         {
             if( nextEnemy!=this )
             {
                 Vector3 nextPos = nextEnemy.transform.position;
                 float nextDist = Vector3.Distance( myPosition , nextPos );
                 if( nextDist<nearestOtherEnemyDistance )
                 {
                     nearestOtherEnemy = nextEnemy;
                     nearestOtherEnemyDistance = nextDist;
                     nearestOtherEnemyPosition = nextPos;
                 }
             }
         }
 
         // decide what to do about it:
         if( nearestPlayer!=null )
         {
             _navMeshAgent.SetDestination( nearestPlayerPosition );
         }
     }
 }

Player.cs:

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 [DisallowMultipleComponent]
 [RequireComponent( typeof(NavMeshAgent) )]
 public class Player : MonoBehaviour
 {
     public static List<Player> Instances = new List<Player>(1);
     [SerializeField] NavMeshAgent _navMeshAgent;// just fill manually and be done with it
     void OnEnable ()
     {
         Instances.Add( this );
     }
     void OnDisable ()
     {
         Instances.Remove( this );
     }
     void Update ()
     {
         
     }
 }
Comment
Add comment · Show 13 · 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 kuba86699 · Nov 11, 2020 at 04:55 PM 0
Share

I needed some time to understand the script you showed. I modified my code based on the one you uploaded. But it's still the same. For some reason, one of the bots is always not moving. And if there's only one bot, it doesn't move either

avatar image andrew-lukasik kuba86699 · Nov 11, 2020 at 07:14 PM 0
Share

W przykładzie powyżej jest takie miejsce:

      // decide what to do about it:
      if( nearestPlayer!=null )
      {
          _nav$$anonymous$$eshAgent.SetDestination( nearestPlayerPosition );
      }

Każe to iść temu nav$$anonymous$$eshAgent-owi do najbliższego Player-a. Gdy na scenie go nie ma to nic nie robi teraz. Także spróbuj wstawić na scenę Playera.

Player, Enemy

avatar image andrew-lukasik kuba86699 · Nov 11, 2020 at 07:23 PM 0
Share

Aha, upewnij się jeszcze, że na scenie masz już Nav $$anonymous$$esh (geometria po której chodzi każdy Nav$$anonymous$$eshAgent)

avatar image kuba86699 andrew-lukasik · Nov 11, 2020 at 07:46 PM 0
Share

Nav $$anonymous$$esh was on the scene practically from the beginning of the project, as was the player's object. Even so, some bots don't move. I went back to this code: using System; using System.Collections; using System.Collections.Generic; using System.Data; using UnityEditorInternal; using UnityEngine; using UnityEngine.AI; using UnityEngine.Scene$$anonymous$$anagement;

 public class NewBehaviourScript : $$anonymous$$onoBehaviour
 {
 
     Nav$$anonymous$$eshAgent _nav$$anonymous$$eshAgent;
 
     private GameObject player;
     private GameObject ai;
 
     private float aiPlayerDist;
     private float aiAiDist;
 
     void Start()
     {
         _nav$$anonymous$$eshAgent = this.GetComponent<Nav$$anonymous$$eshAgent>();
 
         if(_nav$$anonymous$$eshAgent == null)
         {
             Debug.LogError("ERROR" + gameObject.name);
         }
     }
 
     void Update()
     {
         SetDestination();
     }
     
     private void SetDestination()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         ai = GameObject.FindGameObjectWithTag("Ai");
 
         aiPlayerDist = Vector3.Distance(player.transform.position, transform.position);
 
         if (ai.transform.position != transform.position)
         {
             aiAiDist = Vector3.Distance(ai.transform.position, transform.position);
         }
 
         if (player != null || ai != null)
         {
             if (aiPlayerDist > aiAiDist)
             {
                    Vector3 targetVector = ai.transform.position;
                    _nav$$anonymous$$eshAgent.SetDestination(targetVector);
             }
             else if (aiPlayerDist < aiAiDist)
             {
                 Vector3 targetVector = player.transform.position;
                 _nav$$anonymous$$eshAgent.SetDestination(targetVector);
             }
         }
         Debug.Log(aiAiDist);
     }
 }

Some bots work fine, some bots stop working after a few moves and some bots don't come out of spawn at all

Show more comments
avatar image andrew-lukasik · Nov 11, 2020 at 08:27 PM 0
Share

Here is a super simple Follower.cs script that should always work (one navmesh etc is in order):

 [Disallow$$anonymous$$ultipleComponent]
 [RequireComponent( typeof(Nav$$anonymous$$eshAgent) )]
 public class Follower : $$anonymous$$onoBehaviour
 {
     [SerializeField] Nav$$anonymous$$eshAgent _nav$$anonymous$$eshAgent;
     [SerializeField] GameObject _target;
 
     void Update ()
     {
         if( _target!=null )
         {
             _nav$$anonymous$$eshAgent.SetDestination( _target.transform.position );
         }
     }
 }
avatar image andrew-lukasik · Nov 11, 2020 at 09:45 PM 0
Share

I simplified the code a bit, try it now. It's 3 scripts

Bot.cs for those bots:

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 [Disallow$$anonymous$$ultipleComponent]
 [RequireComponent( typeof(Nav$$anonymous$$eshAgent) )]
 public class Bot : Agent
 {
     new public static List<Bot> Instances = new List<Bot>(1);
     [SerializeField] Nav$$anonymous$$eshAgent _nav$$anonymous$$eshAgent;// fill manually
     const float _aiUpdateRate = 0.1f;// [seconds]
     
     void OnEnable ()
     {
         Agent.Instances.Add( this );
         Bot.Instances.Add( this );
 
         InvokeRepeating( nameof(AI_update) , _aiUpdateRate , _aiUpdateRate );
     }
 
     void OnDisable ()
     {
         Agent.Instances.Remove( this );
         Bot.Instances.Remove( this );
 
         CancelInvoke( nameof(AI_update) );
     }
 
     void AI_update ()
     {
         Vector3 myPosition = transform.position;
 
         // find nearest agent:
         Agent nearestAgent = null;
         float nearestAgentDist = float.$$anonymous$$axValue;
         foreach( var nextAgent in Agent.Instances )
         {
             if( nextAgent!=this )
             {
                 float nextDist = Vector3.Distance( myPosition , nextAgent.transform.position );
                 if( nextDist<nearestAgentDist )
                 {
                     nearestAgent = nextAgent;
                     nearestAgentDist = nextDist;
                 }
             }
         }
 
         // decide what to do:
         if( nearestAgent!=null )
         {
             _nav$$anonymous$$eshAgent.SetDestination( nearestAgent.transform.position );
         }
     }
 
     #if UNITY_EDITOR
     void OnDrawGizmos ()
     {
         if( Application.isPlaying && _nav$$anonymous$$eshAgent!=null && _nav$$anonymous$$eshAgent.isActiveAndEnabled )
         {
             Gizmos.DrawLine( transform.position , _nav$$anonymous$$eshAgent.destination );
         }
     }
     #endif
 }

Player.cs for, well, player:

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 [Disallow$$anonymous$$ultipleComponent]
 [RequireComponent( typeof(Nav$$anonymous$$eshAgent) )]
 public class Player : Agent
 {
     new public static List<Player> Instances = new List<Player>(1);
     [SerializeField] Nav$$anonymous$$eshAgent _nav$$anonymous$$eshAgent;// fill manually
     void OnEnable ()
     {
         Agent.Instances.Add( this );
         Player.Instances.Add( this );
     }
     void OnDisable ()
     {
         Agent.Instances.Remove( this );
         Player.Instances.Remove( this );
     }
 }

And an abstract Agent.cs class that both Bot.cs and Player.cs will use:

 using System.Collections.Generic;
 using UnityEngine;
 
 public abstract class Agent : $$anonymous$$onoBehaviour
 {
     public static List<Agent> Instances = new List<Agent>();
 }
avatar image andrew-lukasik andrew-lukasik · Nov 11, 2020 at 09:48 PM 0
Share

It will make bots that do what you wrote they suppose to do - they will follow nearest agent, Player and Bot alike.

avatar image kuba86699 andrew-lukasik · Nov 11, 2020 at 10:36 PM 1
Share

Thanks a lot. Everything works now. You helped me with a problem that I have been trying to solve for several days. I really don't know how to thank you. I'm very grateful. You also showed me some things that I didn't know about, thanks for that too

avatar image andrew-lukasik kuba86699 · Nov 11, 2020 at 11:17 PM 0
Share

You're welcome.

And excuse some of my answers in Polish; your nickname seemed exactly like someone from PL would choose. Cheers.

Show more comments
avatar image
0

Answer by Duckocide · Nov 11, 2020 at 02:35 PM

Check the objects you have found are not the same as the one running this script?

 if (ai.transform != this.transform)

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 kuba86699 · Nov 12, 2020 at 04:31 PM 0
Share

Instead of this aiAiDist = Vector3.Distance(ai.transform.position, transform.position); I wrote it if (ai.transform != this.transform) { aiAiDist = Vector3.Distance(ai.transform.position, this.transform.position); } All the code now looks like this

using System.Collections; using System.Collections.Generic; using System.Data; using UnityEditorInternal; using UnityEngine; using UnityEngine.AI; using UnityEngine.Scene$$anonymous$$anagement;

   public class NewBehaviourScript : $$anonymous$$onoBehaviour
   {
   
       Nav$$anonymous$$eshAgent _nav$$anonymous$$eshAgent;
   
       private GameObject player;
       private GameObject ai;
   
       private float aiPlayerDist;
       private float aiAiDist;
   
       void Start()
       {
           _nav$$anonymous$$eshAgent = this.GetComponent<Nav$$anonymous$$eshAgent>();
   
           if(_nav$$anonymous$$eshAgent == null)
           {
               Debug.LogError("ERROR" + gameObject.name);
           }
       }
   
       void Update()
       {
           SetDestination();
       }
   
       private void SetDestination()
       {
           player = GameObject.FindGameObjectWithTag("Player");
           ai = GameObject.FindGameObjectWithTag("Ai");
   
           aiPlayerDist = Vector3.Distance(player.transform.position, transform.position);
           if (ai.transform.position != this.transform.position)
         {
             aiAiDist = Vector3.Distance(ai.transform.position, this.transform.position);
         }
   
           if (player != null || ai != null)
           {
               if (aiPlayerDist > aiAiDist)
               {
                  Vector3 targetVector = ai.transform.position;
                  _nav$$anonymous$$eshAgent.SetDestination(targetVector);
               }
               else if (aiPlayerDist < aiAiDist)
               {
                   Vector3 targetVector = player.transform.position;
                   _nav$$anonymous$$eshAgent.SetDestination(targetVector);
               }
           }
   
           Debug.Log(aiAiDist);
   
       }
   }

Unity shows me such an error " NullReferenceException: Object reference not set to an instance of an object NewBehaviourScript.SetDestination () (at Assets/test/Scripts/NewBehaviourScript.cs:50) NewBehaviourScript.Update () (at Assets/test/Scripts/NewBehaviourScript.cs:40) "

Follow this Question

Answers Answers and Comments

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

Distance thing with gameobject ? 4 Answers

EnemyAI Script help 1 Answer

Tagged Player working on one character but not the other 0 Answers

How to make multiple targets? 1 Answer

2D Enemy AI X-way only 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