Search code examples
unity-game-engineartificial-intelligenceunityscriptpath-finding

Unity Pathfind simple AI


I have written a simple game in Unity and I need help with some simple AI for enemies.

I have three GameObjects: Background,Player, and Enemy. All these objects have some sprites,rigidBodies etc. and Background contains Box/Circle Colliders. Player can move by pressing W/S/A/D. I need simple script in JS to allow Enemies to follow the Player (with path-finding). I tried something like this, but something has been wrong:

var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
agent.SetDestination(targetPoint);

nothing happens...

Any help please?


Solution

  • Here is a very basic snippet that I have used in enemy movement which I placed into the Update() function of a movement script (Mind the C#):

    Vector3 direction = Vector3.Normalize(transform.position - destination.position);
    transform.position = Vector3.MoveTowards(transform.position, destination.position, moveSpeed * Time.deltaTime);
    

    I hope it can help in getting your enemies to move around!