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 FabienTT · Mar 09, 2013 at 01:12 AM · areadistributiontransform.transformpoint

Distributing agents over an area

Hello,

I have multiple agents (hundreds) in my scene, heading to different areas. An area is a GameObject with a cube and a mesh renderer, material is translucent.

At start each agent have one (random choice) of the area.transform.position as it's destination. Once it gets closer to the area the agent will (should) go somewhere random in the area. It was working properly with axis aligned areas but wasn't working when the area was rotated on Y. I did as it follows : (GoalID is a var where I put the gameObject with the area the agent is going to)

 if(!DestinationReached)
     {
         if((this.transform.position - GoalID.transform.position).sqrMagnitude < 15*15 && GoalID.tag == "Goal") 
         {
         
             GetComponent(NavMeshAgent).destination = GoalID.transform.TransformPoint(Vector3(Random.Range(0,GoalID.transform.localScale.x),0,Random.Range(0,GoalID.transform.localScale.z)) - GoalID.transform.localScale/2);
 
             Debug.Log(GetComponent(NavMeshAgent).destination); //it works, so this part of code is achieved.
             
             DestinationReached = true; //Was false before, then get true sp this part of the code only happens once.
         }
     }

When i do so, all my agents are heading to the same point. Before i was using this line :

 GetComponent(NavMeshAgent).destination = (Vector3(GoalID.transform.position.x + (Random.Range(-GoalID.transform.localScale.x / 2, GoalID.transform.localScale.x / 2)), GoalID.transform.position.y, GoalID.transform.position.z + (Random.Range(-GoalID.transform.localScale.z / 2, GoalID.transform.localScale.z / 2))));

But as i said, it wasn't working with rotated areas, my destination was sometime pointing outside the area... If you have an idea, i'll be happy to try and to understand where i am wrong.

Thank you very much. Fabien.

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

2 Replies

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

Answer by FabienTT · Mar 09, 2013 at 03:23 PM

Finally :

 GetComponent(NavMeshAgent).destination = GoalID.transform.TransformPoint(Vector3(Random.Range(-0.5f, 0.5f),0,Random.Range(-0.5f, 0.5f)));

Did the job perfectly. I was changing the scale but since TransformPoint is doing it, it wasn't necessary. Thank you for your help.

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 MountDoomTeam · Mar 09, 2013 at 09:12 AM

so you need to find a formula to mark the bounds of the area if it is rotated on the y-axis.

on this page is the formula for a box

http://www.econym.demon.co.uk/isotut/simple.htm

You can use it for a rectangle as well, and square, it's basically the same without y-axis and you add a X/Z multiplier for rectangles.

when you have a form of the square, you can say a condition so that the agent only stays in areas of the square equals smaller than one or something like that. Update the function that tells the agent where in the square he is.

Normally it's a bit easier using distance than squares, and invisible Colliders, just make around areas, and oblige the agency stay in the area using distance.

you can make an invisible Collider that only the agent bumps into. Using some kind of selective Collider thing

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 FabienTT · Mar 09, 2013 at 11:52 AM 0
Share

Not sure if I understood, actually I want to get a random position inside a rectangle area (on x and z axis) even if the rectangle is being rotated on y-axis. As you stated in your first sentence. i don't see how i could use the rectangle formula in your link to handle the rotation of my rectangle.

Am sorry am not very good in math and geometry, anyway, thank you for your help.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefabs not finding MonoBehaviours moved into a plugin DLL 1 Answer

Distributing and Updating our Unity MMO causes 'Failed to load PlayerSettings' error. 0 Answers

creating active objects or spots otherwise said HotSpots 1 Answer

Application.LoadLevel When inside a area. (Only script) 1 Answer

Microphone Area Reverb 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