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
1
Question by Loki18000 · May 11, 2016 at 10:03 AM · c#ainavmeshagentpathfinding

NavMeshAgent Finding Alternative Route

I am working on a project where the AI can react to the player.

I have a simple AI built for the Alien Enemy

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class basic_alien_ai : MonoBehaviour {
 
     private NavMeshAgent alien_nav;
     public Transform point;
     public float hit_points = 20;
     private bool attacking_enemy;
     private GameObject current_target;
     public LayerMask alien_layer;
 
     void Awake()
     {
         alien_nav = GetComponent<NavMeshAgent>();
         current_target = point.gameObject;
     }
 
     void Update()
     {
         print(attacking_enemy);
         if (hit_points <= 0)
         {
             Destroy(gameObject);
 
         }
         try
         {
             alien_nav.SetDestination(current_target.transform.position);
         }catch(Exception e)
         {
             print("error");
         }
 
         if (current_target.Equals(null) || current_target == null)
         {
             attacking_enemy = false;
             current_target = point.gameObject;
         }
 
         if (attacking_enemy == false)
        {
             transform.LookAt(current_target.transform.position);
         }
 
         
 
         
 
         RaycastHit hit;
 
 
         if (Physics.Raycast(transform.position, transform.forward, out hit, 20, ~alien_layer))
         {
             if (hit.collider.tag == "enemy")
             {
                 attacking_enemy = true;
                 current_target = hit.collider.gameObject;
 
             }
         }
     }
 
     void OnCollisionEnter(Collision alien_hit)
     {
         if(alien_hit.gameObject.tag == "enemy")
         {
             Destroy(alien_hit.gameObject);
         }
     }
 }
 

This will make the Alien go to the Destination and also look for any player owned units. If it finds one it will go toward it, then it will continue on.

I wanted to know if there was away so the Alien could find another way to the destination(if there is one)

In other words the NavMeshAgent looks for the next possible way to its destination if there is one. I have yet to come up with anything, or if it is even possible with the NavMesh.

Thank you for any help.

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 M-Hanssen · May 11, 2016 at 10:35 AM

Be careful! you are Setting the destination of the enemy EACH frame! You should set it only once, so check if the NavmeshAgent has a path or something before assigning a new destination. Use something like this:

 if (!alien_nav.hasPath)
 {
     alien_nav.SetDestination(current_target.transform.position);
 }

I do not understand your question exactly. Are you trying to find a way to make the navmeshAgent find a different path than the default shortest path?

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 AarshSinghVishen · Jun 19, 2017 at 02:15 AM 0
Share

"Are you trying to find a way to make the navmeshAgent find a different path than the default shortest path?"

Yes, how would you accomplish this?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AI pathfinding waypoints 1 Answer

is it possible to use navmesh on flying character and target ? 0 Answers

Editing NavMesh Paths? 0 Answers

Adding Wander option to AI 1 Answer

NavMeshAgent/Timer Countdown not working,Nav MeshAgent/ Timer stops working 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