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 Sylon87 · May 18, 2019 at 03:27 PM · script.ainavigationai problems

help with not working AI system

hello!

i have some trouble to trying to build a simple Ai system where the NPC is Run away from the player if it's facing him otherwise it will try to go to the player position to eat him

but it doesn't work as i want, it will run away but will freeze to the corner of map, and will not go to eat player if it's not facing him... what i'm missing up?

  using UnityEngine;
  using System.Collections;
  
  public class TunAway : MonoBehaviour {
  
      private Transform player;
      private UnityEngine.AI.NavMeshAgent myNMagent;
      private float nextTurnTime;
      private Transform startTransform;
  
      public float multiplyBy;
  
  
      // Use this for initialization
      void Start () {
  
          player = GameObject.FindGameObjectWithTag ("Player").transform;
          myNMagent = this.GetComponent<UnityEngine.AI.NavMeshAgent> ();
  
          RunFrom ();
      }
      
      // Update is called once per frame
      void Update () {
  
          
          //if is not facing got to eat him
         if(!ISFacing())
         {
             GotToEat();
             Debug.Log("is not facing here");
             
         }
         else
         {
             
             RunFrom ();
         }
      }
  
      public void RunFrom()
      {
  
 
          startTransform = transform;
          
          transform.rotation = Quaternion.LookRotation(transform.position - player.position);
  
          Vector3 runTo = transform.position + transform.forward * multiplyBy;
          
          UnityEngine.AI.NavMeshHit hit;    
 
 
          UnityEngine.AI.NavMesh.SamplePosition(runTo, out hit, 20, 1 << UnityEngine.AI.NavMesh.GetAreaFromName("Walkable")); 
 
          transform.position = startTransform.position;
          transform.rotation = startTransform.rotation;
 
          myNMagent.SetDestination(hit.position);
      }
 
      private void GotToEat()
      {
          myNMagent.SetDestination(player.transform.position);
      }
      private bool ISFacing()
      {
          float dot = Vector3.Dot(transform.position,(player.position - transform.position).normalized);
          if( dot < 0.79f)
          {
              return true;
          }
          else
          return false;
      }
  }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kougasama · May 18, 2019 at 03:47 PM

You might want to check if hit actually is something other than null. Other than that I can't see anything wrong either. Too bad navmesh doesn't automatically get the closest point walkable.. had the very same issue too, with npc getting stuck in corners. No matter what corners.

If it's just the map's end where they get stuck, maybe make them turn left or right when they reach there and no hit on walkable navmesh is detected.

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 Sylon87 · May 19, 2019 at 12:22 AM 0
Share

yes, well i wasn't able to fix it ye.. same for the gotoeat function.. sometimes work sometimes not, i just change it witch compare angle function ins$$anonymous$$d dot product

  using UnityEngine;
  using System.Collections;
  using UnityEngine.AI;
  
  public class TunAway : $$anonymous$$onoBehaviour {
  
      private Transform player;
      private UnityEngine.AI.Nav$$anonymous$$eshAgent myN$$anonymous$$agent;
      private float nextTurnTime;
      private Transform startTransform;
  
      public float multiplyBy;
  
  
      // Use this for initialization
      void Start () {
  
          player = GameObject.FindGameObjectWithTag ("Player").transform;
          myN$$anonymous$$agent = this.GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent> ();
  
          RunFrom ();
      }
      
      // Update is called once per frame
      void Update () {
  
          
          //if is not facing got to eat him
         if(!ISFacing())
         {
             GotToEat();
             Debug.Log("is not facing here");        
         }
         else
         {
             RunFrom ();
         }
      }
  
      public void RunFrom()
      {
  
 
          startTransform = transform;
          
          transform.rotation = Quaternion.LookRotation(transform.position - player.position);
  
          Vector3 runTo = transform.position + transform.forward * multiplyBy;
          
          Nav$$anonymous$$eshHit hit;    
 
 
          
          if(Nav$$anonymous$$esh.SamplePosition(runTo, out hit, 50, 1 << Nav$$anonymous$$esh.GetAreaFromName("Walkable")))
          {
             transform.position = startTransform.position;
             transform.rotation = startTransform.rotation;
 
             myN$$anonymous$$agent.SetDestination(hit.position);
          }
 
 
      }
 
      private void GotToEat()
      {
          transform.rotation = Quaternion.LookRotation(player.position - transform.position);
          myN$$anonymous$$agent.SetDestination(player.transform.position);
      }
      private bool ISFacing()
      {
          float angle = 45;
         float angleToPlayer = Vector3.Angle(player.transform.forward, transform.position - player.transform.position);
  
          if( angleToPlayer < angle)
          {
              return true;
          }
          else
          return false;
      }
  }
 

avatar image
0

Answer by Sylon87 · May 19, 2019 at 01:20 AM

when i try to check if is hitting something with this `

     bool RandomPoint(float range, out Vector3 result)
     {
         for (int i = 0; i < 30; i++)
         {
             Vector3 fixedPoint = transform.position + transform.forward * multiplyBy;
             NavMeshHit hit;
             if (NavMesh.SamplePosition(fixedPoint, out hit, 10.0f,1 << NavMesh.GetAreaFromName("Walkable")))
             {
                 result = hit.position;
                 return true;
             }
         }
         result = Vector3.zero;
         return false;
     }


it will return always true also if is on corners and is facing forward

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

187 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

Related Questions

How do I stop an Enemy NavMeshAgent from intersecting the player model? 3 Answers

How to allow multiple nav mesh agents with different slope climbing abilities to use the same nav mesh? 0 Answers

Why doesn't my AI collide with walls? 0 Answers

NavMesh run away in enclosed space 0 Answers

NavMeshAgent not moving 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