- Home /
AI to make an object wander around and avoid obstacles?
Hello,
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.
Answer by pyro · Jul 20, 2010 at 02:24 PM
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)
I'll check this out and see what I can come up with and then share the final script in this thread. Thank you!
Answer by Andrew 4 · Jul 20, 2010 at 04:23 AM
A* might be a good place to start looking: http://www.arongranberg.com/unity/a-pathfinding/
Thank you for your response, but I have already seen this site. It is good path finding indeed, but all that I need is to make a cube or sphere or whatever wander around aimlessly. In that site you have him go to the spot that was clicked.
@Andrew Ty for the link to Aron's work. I hadn't seen it before. Very sweet.
@Panamamigo, the A* pathfinding system doesn't just allow for click-to-navigate-- you will have to implement your own system for the AI to choose a point to walk to, but the pathfinding system will handle the navigation to that point.
Answer by {RT}_Jester · Jan 13, 2012 at 07:52 PM
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: http://bit.ly/17cQGh0
We have a lot of support resources to help you create characters that drive your story's narrative. http://bit.ly/1bczozr
FAQs to learn more about RAIN as well as other answers http://bit.ly/Hmx9Rn
Email me jester@rivaltheory.com if you have any questions.
Answer by Panamamigo · Jul 21, 2010 at 11:47 PM
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; Debug.Log(wayPoint); }
function Update() { transform.LookAt(wayPoint); 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.