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 zri5004 · Jun 11, 2018 at 01:34 PM · collisionnavmeshagentnavigation

Stopping NavMeshAgent Movement On Collision

So I have a model that patrols an area using NavMesh who also stops (using isStopped) when colliding with the player and turns to look at them. This worked fine in my one project, but when I used the same exact method for another project, he would register the collision, look at the player, then keep walking towards the center of the player collision capsule.

I've tried to alter the navigation code by freezing RigidBody positions, disabling NavMeshAgent component, and setting the velocity to zero, but none have fixed it. If anyone could give me some sort of clue as to what they think might be wrong, that would help so much!

TLDR: NPC-Player Collision works, NPC Navigation works, but NPC starts following the player model's center when collision is triggered. What may be interfering with my code that is causing this?

Here's my code for the navigation:

 public class MoveToPoint : MonoBehaviour {
     public Transform[] waypoints;
     private int destination = 0;
     private Animator anim;
     private NavMeshAgent agent;
     public CapsuleCollider pcCollider;
 
     void Start() {
         anim = GetComponent<Animator>();
         agent = GetComponent<NavMeshAgent>();
         agent.autoBraking = false;
         GotoNextPoint();
     }
     void GotoNextPoint() {
         if (waypoints.Length == 0)
             return;
         agent.destination = waypoints[destination].position;
         destination = (destination + 1) % waypoints.Length;
     }
     void Update() {
         anim.SetFloat("Forward", agent.velocity.magnitude);
         if (!agent.pathPending && agent.remainingDistance < 0.5f) {
             GotoNextPoint();
         }
     }
     void OnTriggerEnter(Collider pcCollider) {
         agent.isStopped = true;
         transform.LookAt(pcCollider.transform);
     }
     void OnTriggerStay(Collider pcCollider) {
         agent.isStopped = true;
         transform.LookAt(pcCollider.transform);
     }
     void OnTriggerExit(Collider pcCollider) {
         agent.isStopped = false;
     }
 }
Comment
Add comment · Show 1
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 bakir-omarov · Jun 11, 2018 at 02:01 PM 0
Share

Share your code pls.

2 Replies

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

Answer by zri5004 · Jun 11, 2018 at 07:29 PM

I fixed it. The walk animation was continuing even though the pathing stopped, so he would just keep walking towards the player. I added 1D blend tree for idle-walking transition, and my code worked.

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
avatar image
1

Answer by bakir-omarov · Jun 11, 2018 at 03:32 PM

I checked your code, just changed few lines. It is working for me. Tag the main player as a Player , and set the Agent's Collider as a Trigger : alt text


Code:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class UnityAnswers_NavmeshStopping : MonoBehaviour {
 
     public Transform[] waypoints;
     private int destination = 0;
     private Animator anim;
     private NavMeshAgent agent;
 
     void Start()
     {
         anim = GetComponent<Animator>();
         agent = GetComponent<NavMeshAgent>();
         agent.autoBraking = false;
         GotoNextPoint();
     }
     void GotoNextPoint()
     {
         if (waypoints.Length == 0)
             return;
         agent.destination = waypoints[destination].position;
         destination = (destination + 1) % waypoints.Length;
     }
     void Update()
     {
         anim.SetFloat("Forward", agent.velocity.magnitude);
         if (!agent.pathPending && agent.remainingDistance < 0.5f)
         {
             GotoNextPoint();
         }
     }
     void OnTriggerEnter(Collider col)
     {
         if (col.transform.CompareTag("Player"))
         {
             agent.isStopped = true;
             agent.velocity = Vector3.zero;
             transform.LookAt(col.transform);
         }
     }
 
     void OnTriggerStay(Collider col)
     {
         if (col.transform.CompareTag("Player"))
         {
             agent.isStopped = true;
             agent.velocity = Vector3.zero;
             transform.LookAt(col.transform);
         }
     }
     void OnTriggerExit(Collider col)
     {
         agent.isStopped = false;
     }
 }



unity-navmesh-stop2.gif (445.8 kB)
Comment
Add comment · Show 4 · 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 zri5004 · Jun 11, 2018 at 03:49 PM 0
Share

I tried it and it does the same thing. Like I said, the collision is triggering, but once triggered the NPC walks toward the player and into the player model. I'm trying to find out what may be conflicting with my code that would be making him do this.

avatar image bakir-omarov zri5004 · Jun 11, 2018 at 03:54 PM 0
Share

Does it stops walking/moving after walking into the player model? Or continuing his way to the next waypoint?

avatar image zri5004 bakir-omarov · Jun 11, 2018 at 03:57 PM 0
Share

Once it triggers the collider, the NPC walks toward the player, ins$$anonymous$$d of standing still. Once you get away from the NPC, he resumes normal pathing to the waypoints.

Show more comments

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

193 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with AI falling through the map using a Character Controller 0 Answers

How to prevent NavMeshAgent from rotate? 1 Answer

Movement causing infinite loops in OnTriggerEnter and OnTriggerExit. 1 Answer

How to create car ai that moves around looking for player 0 Answers

Navigation 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