AI to make an object wander around and avoid obstacles?


I am not asking anyone to write me a script or anything just some sort or pointer or direction to head in. I want to make a script that I would be able to attach to an enemy or something to make it wander or meander around aimlessly and randomly while also avoiding collisions with other objects. Any ideas that I may be able to investigate? Thank you.

A* might be a good place to start looking:

You can use UnitySteer. See the examples.

In order to make it walk around you'd need to basically do 3 things:

1) Pick a random direction (check out Random.insideUnitCircle in scripting help is a good start if its only 2D movement you are after)

2) Send out a ray in that direction, starting from the object and see if it hit anything (check out Collider.Raycast or Physics.Raycast)

3) If the ray did not hit anything, or if it hit something but it is within the acceptable distance away from the object then add some Force to it (if using a Rigidbody component) or lerp the objects transform (iTween or something would help here)

After you made that function you would just randomly call, or call it at an interval to have it meander in different directions. (InvokeRepeating)

RAIN is a, free, complete solution for adding pathfinding, movement, sensing, and behavior to Unity projects.

To download RAIN, just click the link and follow the instructions to get the right version. RAIN:

We have a lot of support resources to help you create characters that drive your story’s narrative.

FAQs to learn more about RAIN as well as other answers

Email me if you have any questions.

So I have been trying to work out a basic little code to make it wander, (without avoiding collisions yet) but it doesn't seem to be working. This is the code I have worked out so far.

var speed : int= 5;
var wayPoint : Vector2;

function Start()
    InvokeRepeating("Wander", 2, 20);

function Wander()
    var wayPoint : Vector2= Random.insideUnitCircle *47;

function Update() 
    rigidbody.AddRelativeForce(Vector3.forward * speed);

It moves if I set wayPoint to a position in the inspector but it moves only to the right and up. This is probably because Vector2 uses only the x and y axis. My problem is that I do not know how to make something move 2 dimensionally across the x and z axis. Any ideas? Why do I have to set it(wayPoint) to a position before it will work? I would've posted this is a comment but then I wouldn't have been able to insert my code.