Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Shindy117 · Feb 10, 2017 at 07:06 PM · fpsrandomspawnvaluesblocks

Choosing given numbers randomly

Hello my fellow Unity people

I got a question and need some help. So I am making a tetris like game to spawn blocks in a FPS like game.

As you can see in the picture my blocks are all over the place in the pit. That is because (as you will see in my code) I am spawning random values.

alt text

So my question is:

" How can I spawn these blocks Randomly between given values: like choosing -4, 0, 4 randomly instead of the numbers that are in between them."

I only want to spawn them randomly at the given values that I specify. That way it isn't all chaotic as you see in the picture. Show me the way! Here is my code:

 public class GameController : MonoBehaviour {
 
     public GameObject[] hazards;
 
     public int hazardCount;
     public float spawnWait;
 
     public Vector3 spawnValues;
 
     void Start()
     {
        StartCoroutine(SpawnBlock());
     }
 
     IEnumerator SpawnBlock()
     {
         for (int i = 0; i < hazardCount; i++)
         {
             GameObject hazard = hazards[Random.Range(0, hazards.Length)];
             Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate(hazard, spawnPosition, spawnRotation);
             yield return new WaitForSeconds(spawnWait);
         }
     }
 }

I thank you in advance

capture.png (389.6 kB)
Comment
Add comment · Show 1
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 Salmjak · Feb 10, 2017 at 07:23 PM 0
Share

Use one or several Random.Range(0,1) then use three if()-statements to deter$$anonymous$$e which the final number should be. $$anonymous$$g. if(probablity < 2/3 && probability > 1/3){ randomSelection = -4;}

1 Reply

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

Answer by jdean300 · Feb 10, 2017 at 08:08 PM

Random.Range is overloaded to work with ints and floats. If you pass it floats, it gives you floats. If you pass ints, it will give you ints. So, just cast your spawnValues coordinates to ints:

 Vector3 spawnPosition = new Vector3(Random.Range((int)-spawnValues.x, (int)spawnValues.x), spawnValues.y, Random.Range((int)-spawnValues.z, (int)spawnValues.z));

This may have some off-by-1 problems, so may need to add one to the result of the Random.Range call. Also note that Random.Range(int, int) is exclusive, meaning it will never return the max value, whereas Random.Range(float, float) in inclusive and can return the max value.

Comment
Add comment · Show 6 · 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 Salmjak · Feb 10, 2017 at 08:28 PM 0
Share

His problem is that he want to choose 3 arbitrary values though... Like -16, 1234214, and 198 with a uniform probability.

avatar image jdean300 · Feb 10, 2017 at 08:46 PM 3
Share
 int[] options = {-16, 1234214, 198};
 
 return options[Random.Range(0,3)];
avatar image elenzil jdean300 · Feb 10, 2017 at 08:59 PM 0
Share

this is the correct approach.

avatar image Shindy117 jdean300 · Feb 10, 2017 at 10:02 PM 0
Share

Thanks ill try this where in the code would this go exactly so I don't screw it up. Just wanna make sure is all. Thank you

avatar image Shindy117 jdean300 · Feb 10, 2017 at 10:13 PM 0
Share

NEVER $$anonymous$$IND this worked perfect thank you so much I just plugged everything in myself. Basically put two and two together lol.

Here is the code just in case, am I right? Its working but I just want to know if there is a clearer method.

  public int[] options = {-4, 0, 4};
     public Vector3 spawnValues;
 
     public Text scoreText;
     public Text gameOverText;
 
     private int score;
     private bool gameOver;
 
     void Start()
     {
         gameOver = false;
         score = 0;
         gameOverText.text = "";
         UpdateScore();
         StartCoroutine(SpawnBlock());
     }
 
     IEnumerator SpawnBlock()
     {
         for (int i = 0; i < hazardCount; i++)
         {
             GameObject hazard = hazards[Random.Range(0, hazards.Length)];
             Vector3 spawnPosition = new Vector3(options[Random.Range(0, 3)], spawnValues.y, options[Random.Range(0, 3)]); 
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate(hazard, spawnPosition, spawnRotation);
             yield return new WaitForSeconds(spawnWait);
         }
     }
avatar image jdean300 Shindy117 · Feb 10, 2017 at 10:43 PM 0
Share

That seems fine. The only thing that may be nice to change is to change Random.Range(0,3) to Random.Range(0, options.Length). That way if you want to add more options all you have to do is add them to the array and everything else will still work. But if you are only going to ever have 3 options, it doesn't really matter.

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

7 People are following this question.

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

Related Questions

How do you make a spawner block? 1 Answer

Spawning objects at random Vector3 3 Answers

Instantiate Random 3D Objects/platforms that dont overlap 1 Answer

Unity crashes after I change 1 symbol in code! 1 Answer

How to randomly replace the main platform with additional ones? 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