Enemy AI following player help
I created a basic AI that follows the player around the map, however since my map is basically a maze with rooms added in, what can I do to prevent it from being stuck in a room, trying to get to my position, even though the player is across the entire map? This is the script, basically saying that when I look at the enemy, it freezes, and when I'm not, it follows my position. The script in its basics work fine, I just need to improve it.
public class EntityBehavior : MonoBehaviour
{
public GameObject Enemy;
public GameObject Player;
public float Speed;
RaycastHit hit;
// Update is called once per frame
void Update()
{
Vector3 forward = transform.TransformDirection(Vector3.forward) * 30;
Debug.DrawRay(transform.position, forward, Color.green);
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit))
{
Enemy.transform.position = Vector3.MoveTowards(Enemy.transform.position, Player.transform.position, Speed * Time.deltaTime);
if(hit.collider.isTrigger)
{
Speed = 0f;
}
else
{
Speed = 1f;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Enemy walking off platform in Unity2D 0 Answers
More advanced Top down enemy AI 0 Answers
I need the Nav Mesh Stop , I need the Animation to Play Javascript 0 Answers
3D enemy pathfinding over moving platform 0 Answers
Foreach working very strange 0 Answers