Question by
Dadohh · Jul 13, 2018 at 05:40 PM ·
2d2d-platformerenemyplatformerenemy ai
Enemy AI ignores the distance from the player
Hi, I'm making a 2D platform and I'd like the enemy to follow the player only within a fixed range. When using this script tho it follow the player regardless of the distance. Could you help me out?
void distanceFromPlayer()
{
float dist = Vector3.Distance(player.transform.position, transform.position);
}
void getToPlayer()
{
if (dist <= 10.0f)
{
Vector3 dir = (player.transform.position - transform.position).normalized;
dir.y = 0.0f; //not changing y dir
transform.Translate(dir * _speed * Time.deltaTime);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to have multiple enemies (currently having to kill them in order) 1 Answer
Having a couple of problems regarding 2D Movement. 1 Answer
Platformer 2D 1 Answer
Boss ai help EoW 0 Answers