Thursday, May 15, 2014

Simple Path Following AI Script in Unity 3D

Hey everyone! Just because this blog is not updated for a very long time, doesn't mean I'm dead. :D Okay, for this first post since 2 years (or more) ago, I am gonna tell you how to create a simple path following AI system in Unity 3D. First of all, create a new project and set up a scene till it look like this:


Our agent is just consist of a capsule and a cube parented to it. The box collider on the cube was removed.


And also, I make the main camera parented to our agent. Just position it till you can achieve a third person camera look.


Then, set up some nodes/waypoints to our scene. Our nodes are just sphere without any collider. Create an empty Game Object and name it Path. Make all of our nodes parented to that Path.


Now, we are ready to code. Our first script will be the Path script. Just create a C# script and name it Path. (Right click on the Assets folder > Create > C# Script)

Our Path script has only an array of Transform and a function to get some certain of node position.
// script by @effendiilham

using UnityEngine;
using System.Collections;

public class Path : MonoBehaviour {

 public Transform[] nodes;

 public Vector3 GetNodePos(int id){
  return nodes[id].position;
 }
}



Attach the Path script to our Path game object on the scene. Then drag each nodes to it.


Then we are ready to create our Agent script.
// script by @effendiilham

using UnityEngine;
using System.Collections;

public class Agent : MonoBehaviour {

 public Path path;
 public float reachDistance = 1f;
 public bool drawGizmos = false;
 public float speed = 5f;
 public float rotSpeed = 10f;
 private int currentNodeID = 0;

 void Start () {
 
 }

 void Update () {
  Vector3 dest = path.GetNodePos (currentNodeID);
  Vector3 offset = dest - transform.position;
  if (offset.sqrMagnitude > reachDistance) {
   offset = offset.normalized;
   transform.Translate (offset * speed * Time.deltaTime, Space.World);
   
   Quaternion lookRot = Quaternion.LookRotation(offset);
   transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, rotSpeed * Time.deltaTime);
  } else {
   ChangeDestNode();
  }
 }

 void ChangeDestNode(){
  currentNodeID++;
  if(currentNodeID >= path.nodes.Length){
   currentNodeID = 0;
  }
 }

 void OnDrawGizmos() {
  if (drawGizmos) {
   Gizmos.color = Color.red;
   Gizmos.DrawLine(transform.position, path.GetNodePos (currentNodeID));
  }
 }
} 


The script above will make our agent follow the current node, and if he is close enough, he will change the destination node to the next node. Now attach the Agent script to our agent on the scene. Don't forget to drag the Path to Agent script.


Then press the Play button and watch our agent following the path. It's very simple Right? :)
You can check the Draw Gizmos option on Agent script to see the current node which is our agent following.


You can download the project file here. That's it. Thank you for reading this tutorial. See you soon on the next post. ;)

5 comment(s):

Sinidomino Neo said...

memberikan yang terbaik ya gan
togel online

Laz Ruiz said...

Thank you :)

Laz Ruiz said...
This comment has been removed by the author.
Unknown said...

This was a great help. Thanks

Muhammad abubaker said...

Can this be used as human traffic in a game?

Post a Comment

feel free to write your comment here.. :)