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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by RmurilloM · Apr 05, 2012 at 07:07 PM · gameobjecttransformarrays

How to assign random positions to gameobjects from an array?

Hi there! I really need your help!!

This is what I'm trying to do:

When my game Start, appear several gameobjects. After a few seconds, each gameobject need to be re-located in a new and random position. An array need to contains those positions. For example, array[0] = transform.position = Vector3(x,y,z);

How can I create an array that contains positions of gameobjects? and How can I assign those positions to each gameobject randomly?

I apologize if my questions is dumb.

Thanks so much in advance!

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 by0log1c · Apr 05, 2012 at 07:18 PM

Here's a short example that returns a random position from a preset array of Vector3. If the position is already in use, it'll attempt to return a free one a few times, if it cannot find any, it'll return positions[0].

 var positions:Vector3[];
 var inUse:boolean[];
 var maxAttempt:int = 10;
 
 function GetPosition():Vector3
 {
     var attempt:int = 0;
     var index:int = 0;
 
     do
     {
         index = Mathf.Random.Range(0,positions.Length);
     }
     while(inUse[index] && ++attempt < maxAttempt);
 
     inUse[index] = true;
     return positions[index];
 }

Unless you want to generate the position randomly from a base position?

 function GetRandomPosition(basePosition:Vector3,radius:float):Vector3
 {
     return basePosition + Vector3(Mathf.Random.Range(-radius,radius),0,Mathf.Random.Range(-radius,radius));
 }
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 RmurilloM · Apr 05, 2012 at 09:09 PM

BY0LOG1C, Thanks so much for you answer!

I'm still wondering, how can I set the array with positions (x,y,z)?

I'm going to explain you exactly what I need, please I apologize because of my English!

When my game Load, a set of "books" (cubes) appears very near from the Camera, and after 10 seconds, each book is going to be re-located in a sideboard/shelf which is further than the books.

What I need is to create an Array that contains those positions: Example: array[0] = transform.position = Vector3(x,y,z)

And then, how can I assign to my gameobjects a random position (which are contain in the array)? Do I need a Script for each gameobject?

I'm not good at Scripting!

Thanks so much again!

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

Instantiate from array into array? 2 Answers

Cannot place checkpoints position into Transform array 1 Answer

How to make an array of GameObjects that stores their transforms? 1 Answer

Instantiate rigidbody, transform or gameobject? 1 Answer

I want to move a cube with rotation but I find this problem 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