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().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.**
↧