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;
}
}
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.
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 :
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;
}
}
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.
Does it stops walking/moving after walking into the player model? Or continuing his way to the next waypoint?
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.