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 trshdnscttrd · Jan 01, 2018 at 08:15 AM · navmeshagent

Nav agent destination stuck at instantiation location

Trying to learn the ins and outs of using the Navmesh, and for the life of me cannot get the agent to update the Destination. It is always stuck as the location it was instantiated at. I have tried using both agent.destination = "" and agent.setdestination, both return the same result. Hoping someone could have a look at the code and identify where ive gone wrong. Code is attached to an instantiated enemy unit.

public class EnemyAI : MonoBehaviour { Animator anim; Rigidbody rbody; NavMeshAgent navAgent;

 [HideInInspector]
 public bool canAttack;
 [HideInInspector]
 public bool canMove;
 private bool attackingPlayer;

 [Header("Movement Properties")]
 public int movementSpeed;
 public int rotationSpeed;

 //[HideInInspector]
 public List<GameObject> NavPointList;   //List array for all local Navpoints
 private GameObject player;              //Reference to Players position
 private Transform myTransform;          //Cacheing this objects transform
 [SerializeField]
 private Transform destination;          //destination for NavMesh Agent
    
 private int RandomNavPoint;
     
 void Start()
 {
     anim = GetComponent<Animator>();
     player = GameObject.FindGameObjectWithTag("Player");
     AddNavMesh();

     canAttack = true;
     canMove = true;
     attackingPlayer = false;

     GetNavPoints();
     NextNavPoint();
     navAgent.SetDestination(destination.position);
     Debug.Log("Actual Destination Position is: " + navAgent.destination);
    
 }

 void AddNavMesh()
 {
     Vector3 sourcePostion = transform.position;                         //The position you want to place your agent
     NavMeshHit closestHit;
     if (NavMesh.SamplePosition(sourcePostion, out closestHit, 500, 1))
     {
         this.transform.position = closestHit.position;            
         gameObject.AddComponent<NavMeshAgent>();
         navAgent = GetComponent<NavMeshAgent>();
         navAgent.autoBraking = false;
     }
     else
     {
         Debug.Log("Could not add Nav Mesh Agent to " + gameObject);
     }
 }
 
 void Update()
 {
     if (canMove)
     {
         if (attackingPlayer == true)
         {
             FollowEnemy();
         }
         if (attackingPlayer == false)
         {
             Roaming();    
         }        
     }
 }

 //All functions for NPC Roaming
 public void GetNavPoints()
 {
     GameObject[] npt = GameObject.FindGameObjectsWithTag("NavPoint");
     
     foreach (GameObject navPoint in npt)
     {
         if (navPoint.transform.parent == transform.parent)
         {
             NavPointList.Add(navPoint);
         }
     }
 }
 
 void Roaming()
 {
     if (Vector3.Distance(transform.position, destination.position) <= 0.5)
     {            
         NextNavPoint();
         navAgent.SetDestination(destination.position);
     }
 }

 void NextNavPoint()
 {
     RandomNavPoint = Random.Range(0, NavPointList.Count - 1);        
     destination = NavPointList[RandomNavPoint].transform;
     Debug.Log("calculated destination position is: " + destination.position);
 }

 //All Functions for Combat / Attacking the player

 void FollowEnemy()
 {

 }

}

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

122 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

Related Questions

Teleporting nav mesh agent in stead of moving 0 Answers

Move character in a destination with OnClick button 0 Answers

How to stop NavMeshAgent from using an area? 1 Answer

Can't get an enemy to chase a player using NavMesh 1 Answer

Nav Mesh: Agent stops for a little while after reaching each target on its path (through agent.setDestination) 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