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 fiorentin · Mar 11, 2013 at 11:38 AM · positionairandom

Random position inside multiple areas

Hi,

I got an object following a Waypoint in 3d space. Every time the player collides with the waypoint, the waypoint moves(teleports) to another location within given range. This is the script im using for the waypoint;

function OnTriggerEnter (myTrigger : Collider) {

  if(myTrigger.gameObject.name == "Sphere"){
   this.transform.position = Vector3(Random.Range(-80, 100), Random.Range(5, 10), Random.Range(-80, 100));
  }
 }

This works fine but i would like to limit the randomness within two different areas. (See image - pink dot is wayponit). If the waypoint is inside Area#1 when it collides with the object it will move to a random position inside Area#2. Then, when it collides with the object inside Area#2 it will move to a random position inside Area#1 and so on.

alt text

I would be most grateful for any ideas, help, advice on this!

/Fredrik

untitled-1.jpg (52.6 kB)
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

Answer by sparkzbarca · Mar 11, 2013 at 12:00 PM

This should be pretty easy

Take and make 2(or infinite) empty game objects which define the (0,0,0) point of a possible AREA.

This allows you to move within a range but to have that range start at that point. Your going to move in the local space of that object (that object will be the center of the universe basically)

To simplify things for later you should probably take and gather all these objects up and make a list of gameobjects

first we'll pick a random area to go to EXCEPT we wont go to the same area we are currently in. You can ignore this step if you want to go in a sequential order. from area 1 to 2 and so on. In that case just iterate through the list instead of picking a random one.

 bool FoundNewArea = false;
 
 gameobject Area;
 int iterator;
 int randomIterator;
 while(!FoundNewArea)
 {
 RandomIterator = random.range(0,ListOfGameobjects.size -1);
 //this next line makes sure we didnt pick to go to where we already are, 
 //again you can remove this if you want a CHANCE you pick the area your already in
 if(iterator != RandomIterator)
 {
 iterator = randomiterator
 FoundNewArea = true;
 }
 }
 
 transform.position = ListOfGameObjects[iterator].transform.position + randomVector;

Of course now each area would be uniform in size you could fix that too but this code would do what you want which is to take whatever area your in. Find a new area out of a list of possible ones. And move to a random location int aht area.

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 sparkzbarca · Mar 11, 2013 at 12:02 PM 0
Share

basically make new areas defined by empty game objects. Collect them in a list and

transform.position = ListOfGameObjects[iterator].transform.position + randomVector;

after that you just decide if picking the new area is random or sequential and if the areas are random in size or uniform and if you can pick the area your in at the moment or not etc

avatar image
0

Answer by fiorentin · Mar 11, 2013 at 01:05 PM

Thank you for your answer! I will have to look into making lists. I really don't know much about code at all but i guess your code is in c#. Can I translate and implement this in my code above so that the action happens when colliding? I guess translating my short one would be easier.

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 fiorentin · Mar 14, 2013 at 10:51 AM 0
Share

Hmm nope cant seem to implement this with the collide code. Any suggestions out there? Would be most appreciated!

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

Spawn GameObjects without overlap 1 Answer

Random Running and Follow at Certain Distance 0 Answers

Random Position For GameObject 1 Answer

[Beginner] Using Random.Range in initial game object position 1 Answer

AI random target 2 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