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
0
Question by Deathslice · Aug 07, 2017 at 04:55 PM · animationrotationrigidbodyanimatoragent

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.

Enemy AI Inspector

enemy.jpg (515.3 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

190 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

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


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