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
0
Question by justking14 · Sep 21, 2014 at 01:24 AM · positiondynamicartificial intelligencewander

Dynamic Wander A.I.

For a project I need to make use of a number of AI techniques, and I'm stuck on dynamic wander. The purpose of this is to create a circle a certain distance in front of the player's orientation and then move to that point and repeat. My current code only seems to move the player in all directions instead of just mostly forward.

 void wand(){
     Vector3 objPos = new Vector3(transform.position.x + (transform.forward.x*10),0,transform.position.z + (transform.forward.z*10));
     Vector3 source =  (Random.insideUnitSphere * 10);
     targetLocation = new Vector3(source.x+objPos.x,0, source.z + objPos.z);
 
 }
Comment
Add comment · Show 2
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 hatake3 · Sep 21, 2014 at 01:56 AM 0
Share

transform.forward is the equivalent of new Vector3(0, 0, 1), so

(transform.forward.x*10) is the equivalent of 0 * 10.

So the vector objPos:

 Vector3 objPos = new Vector3(transform.position.x + (transform.forward.x*10),0,transform.position.z + (transform.forward.z*10));

will have a constant x and y value, while the z value will increase by 10 after every execution.

But you are not moving your object in this code. Where is this method called?

avatar image Kiwasi · Sep 21, 2014 at 04:13 AM 0
Share

@hatake3 You are incorrect here.

transform.forward provides the local forward direction of the transform. You are describing Vector3.forward

Steering behaviour is not meant to move the object (that belongs on the locomotion behaviour). Steering behaviours simply output a direction to travel in.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Sep 21, 2014 at 02:45 AM

Your algorithm isn't correct. You should be using a point on the perimeter of a circle (for 2D movement as you described). You should not be picking a random point each frame, rather you should be moving a random distance around the perimeter of the circle.

The algorithm typically runs like this

  1. Offset the target position from the current target position by a random amount

  2. Project the target position back onto the circle centred on your vehicle

  3. Project the circle in front of the vehicle.

Code to come when I get a moment.

Edit: Code as follows. Should work, untested.

 Vector3 steeringDirection = new Vector3();
 float circleOffset = 5;
 float circleRadius = 2;
 float maxJitter = 0.5f;
 
 void Wander (){
     Vector3 targetPosition = steeringDirection - transform.forward*offset;
     targetPostion = new Vector3 (targetPosition.x + Random.Range(-maxJitter, maxJitter), 0, targetPosition.z + Random.Range(-maxJitter, maxJitter));
     targetPosition.Normalise();
     targetPosition = targetPosition * circleRadius;
     steeringDirection = targetPosition + transform.forward*offset;
 }

The output from this function goes into steeringDirection. You could easily pass it back as a return parameter instead.

You probably want to use invoke repeating or some such to provide frame rate independence. Otherwise this function will produce noticeably different behaviour based on the frequency of recalculation of the steeringDirection.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Keep flies in a sphere? 1 Answer

Dynamically Update Instantiated Objects Position 1 Answer

Projectile Terrain Destruction 1 Answer

how to make enemy ship wander around/patroling ? 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