How to make the AI use the grid
I have a grid which makes a path towards my player but I seem to be stuck as to how to make my enemy move help.
Comment
Answer by Zealot35 · Jun 25, 2018 at 02:38 AM
Here's something I got online:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
//deathDistance is basically just if the players enters this radius the
//enemy will do something to the player, like kill them.
public float deathDistance = 0.5f;
public float distanceAway;
public Transform thisObject;
public Transform target;
private UnityEngine.AI.NavMeshAgent navComponent;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
navComponent = this.gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update()
{
float dist = Vector3.Distance(target.position, transform.position);
if(target)
{
navComponent.SetDestination(target.position);
}
else
{
if(target = null)
{
target = this.gameObject.GetComponent<Transform>();
}
else
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
}
if (dist <= deathDistance)
{
//KILL PLAYER
}
}
}
Make sure you have your player's tag set to "Player" and that you have the Nav Mesh Agent component on the enemy. After that it's just left to tinkering.
Your answer
![](https://koobas.hobune.stream/wayback/20220612165414im_/https://answers.unity.com/themes/thub/images/avi.jpg)