Help with Unity 2D AI
Currently, I'm working on setting up some basic AI for a roguelike 2D game. I have my script working, however I wanted to know if someone has a better way for the detection. Currently I'm using a float (followdistance) and comparing it to the distance between the enemy and the target. However it checks for that comparison in every direction. Does anyone have a way to where it only checks for the distance on the x-axis versus in all directions. Script is below.
{
public float stopDistance;
public float followDistance;
private void Update()
{
if(player != null)
{
if(Vector2.Distance(transform.position, player.position) > stopDistance)
{
if(Vector2.Distance(transform.position, player.position) < followDistance)
{
transform.position = Vector2.MoveTowards(transform.position, new Vector2(player.position.x, transform.position.y), speed * Time.deltaTime);
}
}
}
}
}
Comment