- Home /
About AI moving randomly or use A* Pathfinding
Hi,I am also new in Unity.
I have a question about the character moving randomly.
For example,
an enemy walking in the game and I need it to walk on some path or randomly
I do not know if I would like to get this goal, I have to get some Scripts or use A* Pathfinding.
Chould show me a script keyword or some resources of A* Pathfinding?
Thanks a lot.
And sorry for my poor English.
Answer by ocularcash · Oct 27, 2011 at 03:16 AM
There is resources on this site for A* Pathfinding, just have a look around the site and you'll find a couple resources on it. You can also find alot of pathfinding techniques on youtube. Wandering AI's are a little tricky tho because it can take a long time to write a complex wandering AI script that works good for the environment in your game, Especially if it's an open world where it's wandering. I'll put a very simple free roaming AI script down but remember it's very basic. You'll have to adjust where needed a add a few things depending on your environment. I got this script from steamisM50 but converted it from c# to javascript and taking out some of the stuff you won't need. He has quite a few tutorials on AI so I suggest having a look at his videos.
Wandering AI Script:
var moveSpeed : float = 2;
var rotSpeed : float = 10;
var moveDir : int = 1;
function Update()
{
if(!Physics.Raycast(transform.position, transform.forward, 5))
{
transform.Translate(Vector3.forward * moveSpeed * Time.smoothDeltaTime);
}
else
{
if(Physics.Raycast(transform.position, - transform.right, 1))
{
moveDir = 1;
}
else if(Physics.Raycast(transform.position, transform.right, 1))
{
moveDir = -1;
}
transform.Rotate(Vector3.up, 90 * rotSpeed * Time.smoothDeltaTime * moveDir);
}
}
WayPoint Script:
var waypoints : Transform[];
var speed : int = 2;
private var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
function Start()
{
player = GameObject.FindWithTag("Player").transform;
}
function Update()
{
if(currentWaypoint < waypoints.length)
{
var target : Vector3 = waypoints[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var range : Vector3 = player.position - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1)
{
currentWaypoint++;
}
else if(range.magnitude < 10)
{
velocity = Vector3.zero;
target = player.position;
}
else
{
velocity = moveDirection.normalized * speed;
}
}
else
{
if(loop)
{
currentWaypoint = 0;
}
else
{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
To add the WayPoint script just make as many empty game objects as you need and name them Waypoint1 etc... attach the script to the AI and drag and drop the waypoints.
Your answer
Follow this Question
Related Questions
How to make enemy run animation 3 Answers
collider then change rotation converse? 0 Answers
Mouse as a throttle 1 Answer
move childA to position of childB 1 Answer
Why don't my enemies walk toward me? 1 Answer