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
1
Question by frankyboy450 · Mar 14, 2014 at 09:08 PM · c#transformpositionobjectrandom

moving random objects to random positions

Hello.

I'm attempting to make a small demo game for learning experience. I have 10 different objects which I'm trying to move to a random location depending on the level.

at level one, a total of 6 objects should be moved. 4 of them should be chosen at random and 2 of them should be of the same kind.

at level 2, 8 objects should be moved. 4 random objects and 2 of the same kind.

The function would look like 4+(2*level)

I have a list of all the objects public Transform[] _object;

Does anyone know how to do this with code? I'm guessing I need to call a custom function with the level and then choose an object and make 2 of them at a random position and choose (number - 2) random objects and spawn each of them at a random position.

Hopefully that made sense. Thanks :)

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 shopguy · Mar 14, 2014 at 10:58 PM

Something like this is what you are looking for, or should get you started, I think:

     // Loop for each object, minus 1 because the last one we will duplicate so we have 2 of the same.
     int count = 3 + (2 * LevelNumber);
     for (int i = 0; i < count; i++)
     {
         // Pick our random object type
         int r = Random.Range(0, _object.Length - 1);

         // Ugly code loop to handle the duplicate (change this to better syntax/layout)
         while (true)
         {
             // Create a copy of this object type (I'll leave it to you to figure out how to destroy at end of level, if needed)
             GameObject obj = (GameObject)GameObject.Instantiate(_object[r]);

             // Calculate random values for X,Y,Z and set location of new object
             float x = Random.Range(MinX, MaxX);
             float y = Random.Range(MinY, MaxY);
             float z = Random.Range(MinZ, MaxZ);
             obj.transform.position = new Vector3(x, y, z);

             // If this is our last object, do one more of these inside loops to create 1 more of same time.
             if (i + 1 == count)
             {
                 i++;
                 continue;
             }
             break;
         }
     }


Notes:

_object = an array containing your different types of object, just 1 of each object type.

LevelNumber = you'll have to figure this one out, I keep a global variable in my games and update it anytime I change levels, you can do the same if you want

MinX, MaxX, MinY, MaxY, MinZ, MaxZ = you'll want to define these variables or just replace them with whatever min/max values you want for the spawn locations (i.e.-depends on the max size of your "world"). If you don't want one axis to be random, just make it a fixed value.

Please don't use this code as-is, change it to a better syntax, I just like code I can copy/paste as a single part and not multiple functions for a simple example -- plus, I don't like spending too much time writing perfect code for others to copy/paste vs learn :)

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 frankyboy450 · Mar 15, 2014 at 02:51 AM 0
Share

This looks great. I changed my _Objects variable from Tranform[] to GameObject[] because it gave an error and it seems to be working. I had some trouble with it due the the sorting layer of the sprite not being set so the background was rendered in front of them and I couldn't see the shapes.

Now. If I want to do so the other random objects aren't of the same kind. (So that the same kind doesn't get shown several times) hope that made sense...

Image... In case it didn't make sense. I'm trying to only have one pair of objects. :) I'm guessing I'd have to somehow temporarily remove it from the list...

avatar image shopguy · Mar 17, 2014 at 10:24 PM 0
Share

Are you saying you would want to run the code I gave again (for each level?) and have it not pick the same objects again? Let me know. Off the top of my head I'd say create a .NET List from the _objects array in Start(), and then use that ins$$anonymous$$d of your _objects array, since you can easily remove an item from the list after it is used. Let me know if you need me to update my code to show this.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random array issue C# 2 Answers

Why does object lerp to above the object it is lerping to? 1 Answer

Moving GameObject to various position ? 1 Answer

How to make Camera position Independent of its Rotation? 1 Answer

Multiple Cars not working 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