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 Caiuse · Feb 20, 2011 at 07:22 PM · instantiatepositionvector3random

Randomly position Instantiated GameObject's

I've recently just enquired about randomly instantiating a gameobject, but now I've got a problem randomising there position, I've managed to get the position to randomise but this apples to all the instances. I want each instance to have its own random position.

var grass : GameObject;

function Start() { var grassNum = Random.Range(4, 10); var position = Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10)); for (var i = 0; i < grassNum; i++){ Instantiate(grass,position, Quaternion.identity); } }

Thanks - C

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

1 Reply

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

Answer by johan-skold · Feb 20, 2011 at 07:29 PM

If so you need to generate the position inside the for loop as well, to make sure it runs the same amount of times.

var grass : GameObject;

function Start() { var grassNum = Random.Range(4, 10); for (var i = 0; i < grassNum; i++){ var position = Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10)); Instantiate(grass,position, Quaternion.identity); } }

Comment
Add comment · Show 2 · 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 Caiuse · Feb 20, 2011 at 07:43 PM 0
Share

I don't suppose you know how to change the Quaternion.identity to randomise just on the X, i tried Random.rotation but its on x,y,z? thanks for your answer!

avatar image johan-skold · Feb 21, 2011 at 07:14 AM 0
Share

I assume you mean the X-value that shows up in the inspector? That is, you want to rotate randomly around the X-axis? If so, try; Quaternion.AngleAxis(Random.Range(-180.0f, 180.0f), transform.right)

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

No one has followed this question yet.

Related Questions

Spawn GameObjects without overlap 1 Answer

Instatiating a prefab in a random position 0 Answers

Position an instantiated prefab relative to its translation vector 2 Answers

Instantiate random position except on the player? 1 Answer

Instantiating objects at position 1 Answer


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