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 Mz3D · Jul 11, 2012 at 01:24 PM · instantiatevector3axisrandom.range

Instantiate objects at random positions on axis

What's the code to instantiate 4 clones of an object at 4 different positions on an axis? I tryed

 Instantiate(enemy, Vector3(Random.Range(-10,10),0,15), transform.rotation);

but it doesn't work since the x value, even if it's random, is still a single value. How can i tell Unity to choose four different values (beetween -10 and 10) of the x axis?

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 whydoidoit · Jul 11, 2012 at 01:41 PM 0
Share

Not quite sure what you want here - if you call this code 4 times you will get 4 different values - did you want to ensure that no 2 values were the same?

avatar image Mz3D · Jul 11, 2012 at 02:14 PM 0
Share

I wanted to know if there is another solution than creating 4 different Vector3 values and writing 4 times this code. Anyway, yes, how can i ensure that each of the 4 Vector3 values is different from the other ones?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mz3D · Jul 11, 2012 at 08:51 PM

I wanted to know if there is another solution than creating 4 different Vector3 values and writing 4 times this code. Anyway, yes, how can i ensure that each of the 4 Vector3 values is different from the other ones?

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 TowerOfBricks · Jul 11, 2012 at 03:20 PM

The chance to get two identical floating point numbers from Random.value (or Random.Range) is practically zero. However they might turn out to be really close, so I think you want some margin to make sure it isn't within some distance to the other points.

 int count = 4;
 float range = 10;
 float margin = 1;
 if (count*margin >= range*2) throw new System.Exception ("There is no possible distribution of points");
 Vector3[] arr = new Vector3[count];
 for (int i=0;i<count;i++) {
     bool close = false;
     do {
         close = false;
         arr[i] = new Vector3 (Random.Range (-range,range),0,15);
         for (int j=0;j<i;j++) if ((arr[i]-arr[j]).sqrMagnitude < margin*margin) { close = true; break; }
      } while (close);
      Instantiate(enemy, arr[i], transform.rotation);
 }

This code will create [count] enemies at random positions spaced at least [margin] apart at positions ranging from -[range] to [range] on the x-axis. It will also work if you expand the randomness to more than one axis. The code is written in C#. I hope I haven't made any mistakes (I typed it directly in the browser).

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

6 People are following this question.

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

Related Questions

Object movement - undesired effects 1 Answer

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Randomizing a selection from an array 1 Answer

Are there more options to rotation 'Space' relativity? 1 Answer

Calculate Vector3 for rotation or just change pivot; 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