Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Panamamigo · Jul 20, 2010 at 03:39 AM · collisionaiwanderavoid

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Panamamigo · Jul 20, 2010 at 04:01 PM 0
Share

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

avatar image
4

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/

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Panamamigo · Jul 20, 2010 at 04:40 AM 0
Share

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.

avatar image Julian-Glenn · Jul 20, 2010 at 03:36 PM 0
Share

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

avatar image Andrew 4 · Jul 21, 2010 at 09:53 PM 0
Share

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

avatar image
3

Answer by AliAzin · Jul 20, 2010 at 06:45 AM

You can use UnitySteer. See the examples.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

AI wandering script? 5 Answers

A node in a childnode? 1 Answer

Making AI avoid obstacles and make it wander? 1 Answer

How to make it so enemies only move towards the player when the player is colliding with an object 1 Answer

AI Object Avoidance help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges