Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Xerofa · Sep 05, 2017 at 12:39 PM · enemynavmeshagentchase

Enemy shaking when following player

Alright! I have a issue with my enemy. Whenever I get withing a "chase" distance(which is in float), it it supposed to follow the player. Which it does. However, when it chases the player, it starts to shake/wobble. I am not sure what is causing this but I'll link a video demostrating what I mean. And the code to the EnemyAI as well.

P.S I am using a NavMeshAgent which rotates to the player with transform.LookAt(player) and a transform.forward.(You can see this is void Chase()

-Video: https://streamable.com/fijld (Don't mind the enemy stopping at a waypoint, it's because of the (stopping distance) > (when to go to next point.)

Have a nice day!

 using UnityEngine;
 using UnityEngine.AI;
 
 public class EnemyAI: MonoBehaviour {
     #region Variables
     [Header ("Navigation Variables")]
     public Transform[] waypoints;
     private int wayPointIndex = 0;
     public NavMeshAgent agent;
 
     [Header("Chase Variables")]
     public Transform player;
     public float chaseSpeed;
     public float attackDist;
     public float chaseDist;
     public float stopChaseDist;
     float checkForPlayer = 0;
 
     [Header("Attack Variables")]
     public float damage;
     public float range;
     public Camera enemyCamera;
     public float timeBetweenEnemyAttack;
     float timer;
     #endregion
 
     void Start () 
    {
         agent = GetComponent<NavMeshAgent>();
         agent.autoBraking = false;
    }
     
    void FixedUpdate () 
    {
         timer += Time.deltaTime;
 
         if(!agent.pathPending && agent.remainingDistance < 0.5f)
         {
             GoToNextPoint();
         }
 
         if (player == null)
         {
             FindPlayer();
             return;
         }
 
         float dist = Vector3.Distance(transform.position, player.position);
         if (dist > chaseDist && dist < stopChaseDist) 
         {
             Chase();
             //Debug.Log(dist);
         }
 
         if (dist < attackDist && timer >= timeBetweenEnemyAttack)
         {
             Attack();
         }
    }
 
     public void GoToNextPoint()
     {
         if (waypoints.Length == 0)
             return;
         agent.destination = waypoints[wayPointIndex].position;
         wayPointIndex = Random.Range(0, waypoints.Length);
     }
 
     void Chase()
     {
         transform.LookAt(player);
         transform.position += transform.forward * chaseSpeed * Time.deltaTime;
        // Vector3 targetDir = player.position - transform.position;
         //targetDir.y = 0;
        // Debug.Log("Start chase!");
     }
 
     void Attack()
     {
         timer = 0f;
         RaycastHit hit;
         transform.LookAt(player);
         //Debug.Log("Attack Player!");
         if (Physics.Raycast(enemyCamera.transform.position, enemyCamera.transform.forward, out hit, range))
         {
             Debug.Log(hit.transform.name);
             Debug.DrawLine(enemyCamera.transform.position, hit.point);
             PlayerHealth Player = hit.transform.GetComponent<PlayerHealth>();
             if (Player != null)
             {
                 Player.TakeDamage(damage);
             }
         }
     }
 
     void FindPlayer()
     {
         if (checkForPlayer <= Time.time)
         {
             GameObject searchResult = GameObject.FindGameObjectWithTag("Player");
             if (searchResult != null)
                 player = searchResult.transform;
             checkForPlayer = Time.time + 0.5f;
         }
     }
 }
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

2 Replies

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

Answer by NorthStar79 · Sep 05, 2017 at 01:18 PM

first, you need to add a minimum distance for enemy (at least as much as radius of player) because it can not get exact position what it wants due to colliders, this will cause some serious problems.

after this tweak, here is your possible solution. eighter you need to chase player via navmesh, or you need to disable it before moving it yourself (or changing its acceleration to 0 while it's chasing) i suggest first option. so your code should look like something like this :

 void Chase()
      {
          agent.destination = player.transform.position
      }
Comment
Add comment · Show 2 · 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 Xerofa · Sep 05, 2017 at 01:37 PM 0
Share

The first point you talked about, I'm a little lost at. Do you mean that because of the player collider, the enemy can't get close enough to the point so it starts shaking?

The second point I'll return to when I figure out the first point.

avatar image Xerofa · Sep 05, 2017 at 01:43 PM 1
Share

I got the enemy to shake less when changing to Update ins$$anonymous$$d of FixedUpdate. But I guess that is not good performance wise?

avatar image
0

Answer by Xerofa · Sep 05, 2017 at 02:17 PM

I kinda got it to work! I added the stopping distance and your code above. Now the only thing is that it make a quick stutter when it attacks time to time. Not sure why.

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

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

69 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

Related Questions

Navmesh Agent - stop after player out of range 1 Answer

Enemy's NavMesh not working 2 Answers

Horde of NavMeshAgents - stops to recalculate path. 4 Answers

Tank not moving towards player 1 Answer

Script issues [js] 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