• 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 TheSaviour · Jul 18, 2019 at 02:43 PM · random.rangerandomspawning

Is there a better way to generate random numbers?

I have 9 spawn points (suppose they're called Point0, Point1,.... Point8). The x position of each spawn point is set 0.5 units to the right of the previous spawn point (x position of Point0 is -2, x position of Point1 is -1.5, x position of Point2 is -1 etc). All spawn points are placed in an array (Point0 at index 0, Point 1 at index 1, etc).

My intention is to randomly select a spawn point and spawn an object at that point. I initially used Unity Random.Range function like so:

 spawnPointIndex = Random.Range(0, spawnPointsArray.Length);
 //And then I spawn the object at spawnPointsArray[spawnPointIndex]

However, I noticed that every now and then, the same spawnPointIndex was generated twice or thrice in a row. This is bad random behavior. So I made a modification:

 int prevSpawnIndex = -1 //this is a global variable
 
 do
 {
    spawnPointIndex = Random.Range(0, spawnPointsArray.Length);
 } while (prevSpawnIndex == spawnPointIndex);

And then it worked....kinda. Although the same spawnPointIndex is no longer generated more than once in a row, there is still a flaw. Every now and then, the spawnPointIndex variable creates a pattern like this: 0, 2, 4, 6. Sometimes, the generated values for spawnPointIndex may even follow this order: 8, 7, 4, 3.... In other words, the Random.Range function causes the spawnPointIndex to keep increasing too many times, or keep decreasing too many times. It doesn't seem random enough. And this occasionally causes some extreme situations for the player, where it becomes almost impossible for the player to progress. Here's an example:

alt text

The way the game works is that the platforms keep rising upwards and the player (the green ball) must always land on the yellow platforms. If the ball lands on a red platform or if the player hits the spikes, then the player loses. You can see in the picture that the player is stuck in a situation where the ball can't really land on a yellow platform safely. The movement of the ball is physics-based and that makes it very hard for the player to ensure that the ball will safely land on one of the yellow platforms.

So I was hoping there would be a better way to create random behaviour so that these sort of scenarios could be avoided.

impossible.png (10.8 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 myzzie · Jul 18, 2019 at 05:42 PM 0
Share

Doesn't sound like you want random at all.

You can see in the picture that the player is stuck in a situation where the ball can't really land on a yellow platform safely.

This will always be an issue no matter how random you make it. I think what you want is procedural generation for this.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JonPQ · Jul 18, 2019 at 06:24 PM

I made a similar game ('Jumpkin' in the asset store, please check it out... https://apps.apple.com/us/app/jumpkin/id668410468) and had a similar problem. in the end I gave up on totally random, and instead figured out patterns of nice layouts that worked, then I made a list of these (hand made) then randomized how those fit together. So basically I built layers, or short sections that could all fit next to each other.... with difficulty ratings per section, for player progression... then I grouped them together randomly (also based on distance vs difficulty) (using the same random seed each time, if you want a level to remain the same layout)

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 morvi · Jul 18, 2019 at 06:40 PM

Another idea (hacky but maybe simpler) would be to do 3 or so Random.Range calls and Choose afterwards the one that fits best, following some rules that need to be defined

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

Simple random spawning system for 10 items without duplicates 2 Answers

how can i random a spawning but not same gameobject 1 Answer

that items doesnt spawn in player position 1 Answer

Start ambient audio tracks at random points? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges