- Home /
How do I stop my enemy character from spinning around in circles when I run past him?
I have an enemy NavMashAgent who wanders around the map with the objective being to kill the player. He will only go after the player if he spots the player within a certain range and angle. After the player has been spotted, he will start pursing the player until the player is out of range. Once the player is out of range, the enemy will idle for awhile and resume wandering around the map. This is the entirety of my BasicAI script. The problem that I have is if the player decides simply to run past the enemy, the enemy will start to spin in circles and get stuck in its current attacking animation.
I not sure if it has something to do with me having to freeze the X and Z rotation on the enemy's rigid body to prevent it from floating in the air but that would be my guess. In addition, I also created a new Physics Material and set its dynamic friction and static friction to 1 to prevent the enemy from sliding. Any help would be much appreciated.
Basic AI C# Script
using UnityEngine;
using UnityEngine.AI;
public class BasicAI : MonoBehaviour
{
public Transform player;
public Transform head;
private Animator agentAnimator;
private NavMeshAgent agent;
public float lineOfSightRange;
public float attackingRange;
public float lineOfSightAngle;
public float wanderTimer;
public float wanderRadius;
private bool isPursingPlayer;
public float idleTimer;
private float timer;
// Use this for initialization
void Start()
{
agentAnimator = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
timer = wanderTimer;
isPursingPlayer = false;
}
// Update is called once per frame
void Update()
{
Vector3 direction = player.position - transform.position;
direction.y = 0;
float angle = Vector3.Angle(direction, head.up);
patrol(direction, angle);
if (hasReachedDestination())
{
timer = wanderTimer;
}
}
private void patrol(Vector3 direction, float angle)
{
if (timer >= wanderTimer && !isPursingPlayer && !agentAnimator.GetBool("isIdle"))
{
agentAnimator.SetBool("isWalking", true);
Vector3 newPosition = GenerateRandomPosition(transform.position, wanderRadius * 2 * agent.height);
agent.SetDestination(newPosition);
timer = 0.0f;
}
else if (agentAnimator.GetBool("isIdle") && timer >= idleTimer)
{
agentAnimator.SetBool("isIdle", false);
agent.isStopped = false;
timer = wanderTimer;
}
else
{
float currentDistance = Vector3.Distance(player.position, transform.position);
if (currentDistance < lineOfSightRange && angle < lineOfSightAngle)
{
isPursingPlayer = true;
chase(direction);
}
else
{
if (isPursingPlayer)
{
agentAnimator.SetBool("isIdle", true);
agentAnimator.SetBool("isWalking", false);
agentAnimator.SetBool("isAttacking", false);
agent.isStopped = true;
timer = 0.0f;
}
isPursingPlayer = false;
}
timer += Time.deltaTime;
}
}
public static Vector3 GenerateRandomPosition(Vector3 origin, float dist)
{
Vector3 randDirection = origin + Random.insideUnitSphere * dist;
NavMeshHit navHit;
NavMesh.SamplePosition(randDirection, out navHit, dist, NavMesh.AllAreas);
return navHit.position;
}
private void chase(Vector3 direction)
{
agentAnimator.SetBool("isIdle", false);
if (direction.magnitude > attackingRange)
{
agent.SetDestination(player.position);
agentAnimator.SetBool("isWalking", true);
agentAnimator.SetBool("isAttacking", false);
}
else
{
agentAnimator.SetBool("isWalking", false);
agentAnimator.SetBool("isAttacking", true);
}
}
private bool hasReachedDestination()
{
return agent.remainingDistance < agent.stoppingDistance && !agent.pathPending;
}
}
Here is how Rigidbody and NavMeshAgent look like in the inspector.
Your answer
Follow this Question
Related Questions
Only animate rotation not position 1 Answer
Run smooth continuous rotation animation of spinner using animator 1 Answer
Matching character's rotation to the rotation of a animation - How? 0 Answers
Mirrored animation doesnt rotate correctly 0 Answers
Armature stuck on a single side after an animation 0 Answers