I'm using a script that tells enemies to chase my player character. Problem is, only the first enemy I encounter chases me down - the rest don't move at all when I get into a certain distance of theirs.
All the enemies are the same prefab. I am certain that the script still gets triggered when I get into their detection distance, because their respective animations start playing when triggered - they just aren't moving to me.
My game is 2.5D platformer. (In a 3D environment, but every single movement happens on a left and right axis.)
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
public Transform Player;
static Animator anim;
void Start()
{
anim = GetComponent();
}
void Update()
{
Vector3 direction = Player.position - this.transform.position;
float angle = Vector3.Angle(direction, transform.forward);
if (Vector3.Distance(Player.position, this.transform.position) < 28 && angle < 360)
{
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 1f);
anim.SetBool("isIdle", false);
if (direction.magnitude > 1)
{
this.transform.Translate(0, 0, 0.1f);
anim.SetBool("isWalking", true);
anim.SetBool("isAttacking", false);
}
else
{
anim.SetBool("isAttacking", true);
anim.SetBool("isWalking", false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
}
Any help would be greatly appreciated. Thanks!
↧