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 MCoburn · Nov 18, 2015 at 04:15 AM · c#rotationnavmeshbug-perhaps

NavMesh Agents: They won't stop when you want them X units away from the target and 2 other questions...

I'm getting some weird behaviour from the NavMesh pathfinding that comes with Unity. Yes, I know there are a lot of other options out there including RAIN and other AI solutions that are "better", but I'm trying to keep costs low. I'm using Unity 5.1.3f1 because 5.2.x branch has some bugs with Mechanim and other subsystems (I haven't checked the release notes to see if they have been fixed).

Here's the script I'm using, which automatically assigns a NavMeshAgent item to the object you wish to control:

 using UnityEngine;
 using System.Collections;
 using System;
 
 [RequireComponent(typeof(NavMeshAgent))]
 public class NPCFollowsEntity : MonoBehaviour
 {
     public GameObject targetEntity;
 
     [Range(0.5f, 2.0f)]
     public float closeEnough = 1.0f;
     public bool controlAnimations = true;   // if true, we'll pass the speed into the character's animation controller.
 
     private NavMeshAgent _navAgent;         // our working reference to the navagent
     private Animator _ac;                   // our working reference to the animation controller
 
     private bool moveTowardsEntity = true;  // an attempt to try to get the AI to only move if this was set, but alas it was borked
 
     // Use this for initialization
     void Start()
     {
         // Give them the satnav.
         _navAgent = gameObject.GetComponent<NavMeshAgent>();
 
         if (!_navAgent)
         {
             Debug.LogError("NPC Error: No navmesh agent. Abort.");
             this.enabled = false;
             return;
         }
 
         // If we are to control animations, and they haven't set the animator...
         if (controlAnimations)
         {
             _ac = gameObject.GetComponentInChildren<Animator>();
             if (!_ac)
             {
                 Debug.LogError("NPC Error: No animator yet we want to control animations?");
                 controlAnimations = false;
             }
         }
     }
 
     // 
     void Update()
     {
         // an attempt to make the AI look at the target entity which was kinda iffy.
         // transform.rotation.SetLookRotation(targetEntity.transform.position, transform.up);
     }
     void LateUpdate()
     {
         if (controlAnimations) UpdateAnimationController();
         CheckNPCTargetStatus();
     }
 
     void FixedUpdate()
     {
         /* The following is a failed attempt at shooting a ray to see if the entity we hit is close enough.
         RaycastHit distanceToEntity;
         if(Physics.Raycast(transform.position, transform.forward, out distanceToEntity))
         {
             if (distanceToEntity.distance <= closeEnough) moveTowardsEntity = false;
             else moveTowardsEntity = true;
         } */
     }
 
     private void UpdateAnimationController()
     {
         _ac.SetFloat("speed", _navAgent.velocity.magnitude);    // doesn't work... well it does but it derps around
     }
 
     private void CheckNPCTargetStatus()
     {
         if (moveTowardsEntity) _navAgent.destination = targetEntity.transform.position;
         // else _navAgent.destination = transform.position; <- ignore this
 
         if (_navAgent.stoppingDistance != closeEnough) _navAgent.stoppingDistance = closeEnough;
     }
 }

If you copy and paste that C# script into your project, make a test level, add a capsule as the target, and set the parameters like this:

params to use

And you should get something like this video located here (228KB). The video shows the AI NavAgent "bouncing" or "rubber banding" between like half a unit and the actual player. Applying Root Motion or not doesn't affect the outcome, it always "bounces" like this. Stopping Distance seems to do nothing constructive. Is this a bug?

My question here is what am I doing wrong in the code? Do I need to set the AI destination once and once only, and instead at Start() rather than Update() ?

Secondly, how would I make it so that the NavMesh Agent-controlled entity always looked at the target? As you can see in LateUpdate() I tried to set the look position using SetLookRotation... but I'm not sure that is what I want. Basically, the AI should look at the target entity (in this case, the player) so it doesn't look like the AI is just moon walking.

Last but not least, say for example the player has died, and AIs no longer should follow that target. If I set the target to null, will the AI stop? Or is there a method I can call that will stop the AI? (I guess another way is just to set another position and let the AI chase after that...)

If you manage to get it working, please let me know what you changed. Cheers!

2015-11-18-14-01-18.png (30.6 kB)
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

33 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Distribute terrain in zones 3 Answers

NavAgent not following on rebuilt NavMesh 0 Answers

Multiple Cars not working 1 Answer

Looking forward while walking with navMeshAgent 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