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 /
avatar image
0
Question by HemK · Sep 21, 2020 at 12:09 PM · navmeshagentscriptingproblem

NavMeshAgent does not move to an updated position

Hi
I've got the following script asking a navMeshAgent to move to a position dynamically. What's happening is the Agent moves to the starting location of the Player gameObject and doesn't move or rotate after that, even though the player object has moved.

   public class EnemyMover : MonoBehaviour
     {
     
         public GameObject bullet;
         public NavMeshAgent agent;
         public Transform target;
         public float fireRate;
         private float nextFire;
         public GameObject Player;
         public float fireRange;
         Vector3 LastKnownPos;
         int takeDamage;
         public int EnemyDamage;
         public int HP;
         public Transform shotSpawn1;
         public Transform shotSpawn2;
         Transform shotActive;
         public Transform eye;
         public float range;
         public LayerMask playerLayer;
     
     
         // Start is called before the first frame update
         void Start()
         {
             fireRange = 6f;
             takeDamage = 2;
             HP = 6;
             shotActive = shotSpawn1;
     
         }
     
         void Update()
         {
             //Debug.DrawRay(eye.transform.position, transform.TransformDirection(Vector3.forward)*range);
             //RaycastHit sight;
             //if (Physics.Raycast(eye.transform.position, transform.TransformDirection(Vector3.forward), out sight, range, playerLayer))
             //{ 
                 CustomEvent.Trigger(gameObject, "alert"); 
                 agent.updateRotation = true;
                 agent.SetDestination(Player.transform.position);
                 agent.stoppingDistance = fireRange;
                 //Vector3 lastKnownPos = Player.transform.position;
             
            // }
         }
 
 
 

  
Comment
Add comment · Show 2
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 CodesCove · Sep 21, 2020 at 10:46 PM 0
Share

At least stopping distance is pretty long.. if the agent is within the 6 units it will not move towards the player.. Also set the updateRotation and stoppingDistance in the Start(). They don't need to be set in every frame

avatar image HemK CodesCove · Sep 22, 2020 at 04:39 AM 0
Share

Thank you @CodesCove for the heads up :) I've made the changes you recommended. On a side note, I have tried to move the unit without a stopping distance also, but the Nav$$anonymous$$eshAgent only moves to the player.transform.position when I hit play and stops moving after that. Should I try this with the .destination parameter instead?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HemK · Sep 22, 2020 at 05:05 AM

Made a silly mistake. The NavMeshAgent I wanted to follow was a child object of the target. The Target was at 0,0,0 and the agent in the script was moving to that location, while the intended target was moving about. I simply assigned the child object as the target in the Inspector and problem solved.
I'm not deleting this post in case somebody else makes this noob mistake and wants to save themselves the public embarrassment.

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

138 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

Related Questions

Runaway AI 1 Answer

NavmeshAgent Behavior 0 Answers

No player is colliding with enemy even with capsule collider !!! Please me help me. 0 Answers

NavMeshAgent Not Working? 1 Answer

A way to check if an off mesh link is nearby? 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