- Home /
Enemy Rigidbody vibration when reaching the player
Hello programmers around the world I am having a problem with one of my scripts for the enemy in my game. It is supposed to follow the player to a certain beyond which it actually reaches the player However, when it goes beyond that point it just vibrates around back and forth as if it is colliding with the player. However that is not the case because I change the point from 1.2f from the player to 10.2 and the result was the same as if the player is hitting an invisible wall. Side node: I am not trying to collide with any box or capsule or character colliders so the issue is not there. I am trying to figure how to solve this problem and I've been at it for a month now with rigidbodys, LateUdate(), FixedUpdate() and AddForce so nothing seems to work. At this point any help would be appreciated. Here is my code for following the player:
public float MyAttackRange { get; set; } private Vector3 direction; [SerializeField] private EnemyScript enemy; [SerializeField] private Player player; private Transform target; public float autoAttackCoolDown; public float autoAttackCurrentTime; void Start() { MyAttackRange = 1.2f; } private void FollowTarget() { if(target != null && player.inLineOfSight) { StopCoroutine(EnemyIdle()); float distance = Vector3.Distance(player.transform.position, enemy.transform.position); if(distance > enemy.MyAttackRange) { Vector3 targetDir = player.transform.position - transform.position; float step = runSpeed * Time.deltaTime; Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F); transform.rotation = Quaternion.LookRotation(newDir); direction = (target.transform.position - transform.position).normalized; transform.position = Vector3.MoveTowards(transform.position, target.position, runSpeed * Time.deltaTime); if(distance == enemy.MyAttackRange) { eAnim.SetBool("walk", false); rBody.velocity = Vector3.zero; direction = Vector3.zero; } } if(distance <= enemy.MyAttackRange) { if(player != null) { if(autoAttackCurrentTime < autoAttackCoolDown) { autoAttackCurrentTime += Time.deltaTime; } else { DoDamage(15); player.GetComponent<Player>().health.CurrnetValue -= 15; autoAttackCurrentTime = 0; } } } } else { direction = Vector3.zero; } }
I'm out of options. Any help would be appreciated at this point. Thanks in advance! Remember the goal is for the enemy to stop shaking and vibrating back and forth when he reaches the player.
Your answer
![](https://koobas.hobune.stream/wayback/20220612145431im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy vibrates when it reaches the player 0 Answers
Unity3D : Enemies following the player keep moving through each other 1 Answer
Help with Generating level 0 Answers
My healtbar is not change when i look another enemy 0 Answers
How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer