- Home /
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;
}
}
}
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
}
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.
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612134235im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy's NavMesh not working 2 Answers
Navmesh Agent - stop after player out of range 1 Answer
Enemy prefab with NavMeshAgent change speed in other map? 0 Answers
Enemy follow Camera 0 Answers
Chasing Object Sinking 2 Answers