Advertisement

Scatter/spawn game coins using random with exceptions

Started by August 08, 2023 01:07 PM
4 comments, last by LorenzoGatti 1 year, 4 months ago

Greetings !

I have a game where the player should collect the alphabet coins in its correct order.

map is really big , this is just 1/16 part of it.

Coins you can see are being spawn using Random.Range with min/max X and Y … all this works fine until coin is on CircleCollider (like on letter “C” on the left) where car can't get on to … this means that game is automatically over and player loses because of the thing I have not yet developed.

The first idea for the solution is to put exceptions for each object for coins not to appear on that place … those are trees , buildings , etc … Buildings on my map are over 100 , trees about 100. I don't think that exception is a good idea. Yes I need an idea more here/today and not the code (but I would appreciate if you give me the idea for a code).

How I can spawn my coins that way so they won't be on some objects (I may tag them and then use).

Please assist ?

and thank you in advance

You could approach this in a TDD-like manner. You have your procedure which for each random point places a coin.

You could invent a function for it - a test step - that must return true, or it would have to try another point to place that coin at.

Let's call the map's new function isItOkToPutACoinHere(x, y)

Invoke it from a test, using a known map, and you should then expect it to return false on the tree, and true on the lawn.

Next, you implement that function; for all objects, check, or from a point in a precomputed/maintained map, check.

Advertisement

From the map, the shapes of the trees and the buildings are circle and rectangle. It's not hard to detect if a location is within a circle or rectangle.

None

Solved ! Thanks to everyone

Computing allowed coin locations is quite difficult because the coin must be not only away from obstacles itself, but also reachable for the player (which, if it is a "car", might be unable to slip between close obstacles or to turn).

Maybe you could compute a conservative approximation of the part of the map that can be reached from the player's starting point.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement