x


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.

more ▼

asked Jul 20 '10 at 03:39 AM

Panamamigo gravatar image

Panamamigo
126 13 13 23

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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)

more ▼

answered Jul 20 '10 at 02:24 PM

pyro gravatar image

pyro
760 4 5 23

I'll check this out and see what I can come up with and then share the final script in this thread. Thank you!

Jul 20 '10 at 04:01 PM Panamamigo
(comments are locked)
10|3000 characters needed characters left

You can use UnitySteer. See the examples.

more ▼

answered Jul 20 '10 at 06:45 AM

AliAzin gravatar image

AliAzin
2.5k 41 56 78

(comments are locked)
10|3000 characters needed characters left

A* might be a good place to start looking: http://www.arongranberg.com/unity/a-pathfinding/

more ▼

answered Jul 20 '10 at 04:23 AM

Andrew 4 gravatar image

Andrew 4
61 1 1 6

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.

Jul 20 '10 at 04:40 AM Panamamigo

@Andrew Ty for the link to Aron's work. I hadn't seen it before. Very sweet.

Jul 20 '10 at 03:36 PM Julian Glenn

@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.

Jul 21 '10 at 09:53 PM Andrew 4
(comments are locked)
10|3000 characters needed characters left

RAIN{indie} is a, free, complete solution for adding pathfinding, movement, sensing, and behavior to any Unity project.

To download RAIN{indie}, just click the link and follow the 2 steps. RAIN{indie}: http://rivaltheory.com/downloadindie

We put together a compilation of tutorials and documents from the RAIN{indie} AI community that supplements our own support resources http://bit.ly/W47KSO

FAQs to learn more about the differences between RAIN{one} and RAIN{indie} as well as many other answers. http://support.rivaltheory.com/documentation/faq

Email me jester@rivaltheory.com if you have any questions.

more ▼

answered Jan 13 '12 at 07:52 PM

{RT}_Jester gravatar image

{RT}_Jester
76 1 2 5

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 21 '10 at 11:47 PM

Panamamigo gravatar image

Panamamigo
126 13 13 23

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3717
x2482
x954
x25
x12

asked: Jul 20 '10 at 03:39 AM

Seen: 6832 times

Last Updated: Feb 21 at 12:10 AM