- Home /
2D Enemy AI range problem
Hi, I am currently learning programming and making a 2D rpg game. I've encountered a problem which I haven't been able to fix for 2 whole days.. It's really getting to my nerves now and I can't find any solution. The problem is that the enemy detects the player/target, only if it was spawned near the player. If I spawn it far away and go near it, it doesn't detect my player. And when it has detected, it only moves to the position it detected the player and gets stuck there
Here's a gif:
As you can see, the enemies are moving where I was. It's like the script is only checking the code once. Here's the code:
public class EnemyAI : MonoBehaviour {
[SerializeField]
private float maxSqrDistance = 16, //4 meters
moveSpeed = 1f;
public Transform target;
void Update () {
//Vector between the 2 entities
Vector3 dir = target.position - transform.position;
float distance = dir.sqrMagnitude, //Length squared because a sqrt is expensive
dot = Vector3.Dot(transform.forward, dir.normalized);
if (distance < maxSqrDistance) //Is the distance less than what we want?
{
Debug.Log("Within range!");
//Move in the direction, so towards the player
transform.position += dir.normalized * moveSpeed * Time.deltaTime;
}
}
}
Thanks for reading.
Answer by Limesta · Dec 30, 2014 at 05:19 AM
You don't update public Transform target.
Your AI is going off of the 0'th frame, and any frames afterward are not detected.
So simply add code at the beginning of your update frame that updates the position.
Answer by Regalith · Dec 30, 2014 at 04:22 AM
First off that private variable doesn't need to be serialized. My advice would be to switch the target variable type to GameObject. My guess is it's not updating the reference to your player since a transform was passed in as a public variable. Try this and see what happens:
public class EnemyAI : MonoBehaviour
{
private float maxSqrDistance = 16 //4 meters
public float moveSpeed = 1f; //So you can tweak move speed in the editor or in enemy prefabs
public GameObject target;
void Update () {
//Vector between the 2 entities
Vector3 dir = target.transform.position - transform.position;
float distance = dir.sqrMagnitude, //Length squared because a sqrt is expensive
dot = Vector3.Dot(transform.forward, dir.normalized);
if (distance < maxSqrDistance) //Is the distance less than what we want?
{
Debug.Log("Within range!");
//Move in the direction, so towards the player
transform.position += dir.normalized * moveSpeed * Time.deltaTime;
}
}
}
Finally, why not use Vector2 instead of Vector3? It looks like a 2D game, so you dont need to keep track of a 3'rd axis.
Your answer
Follow this Question
Related Questions
Enemy VISION range? Help! 0 Answers
How to make enemy check if it doesn`t see player for certain times it changes state 1 Answer
How to correctly do an enemy ranged attack script? 0 Answers
How to make a game object detect if any of multiple game objects are in range 2 Answers
Enemy Patrol - Random Walk 1 Answer