Randomly generating coins

Hi,
can you please help me with randomly generating coins on random generated terrain? But not generate all the time just from time to time
I will attach picture that shows my game. I am really stuck with that one so any I would appreciate any help. Thanks!
(btw, it is made on iOS/android)

If you got a collider on the ground, you can check downwards with a raycast in front of your car to get position, every random time between (a, b) using:

Random.Range(a, b) and Coroutines.

And is not there a chance that coin will generate at wrong position? Because the terrain is really “hilly” sometimes

But I need to generate coins before the player can see them (small part of terrain is already generated ahead)

With a raycast you will hit the exact position of the hill ground. You just check with the raycast at the cars position + whatever on the x-axis. You are not forced to use screen space.

You could try dropping them from higher coordinate?

Yes, I was thinking about some complicated solutions and did not think about this, by this time I have already done that. (Raycast was not, for some reason so accurate)

Like BFGames says, you can do a check with a raycast offscreen and cast downwards until your terrain is hit. Then that hitpoint will be the “floor” height for your coins. So for example, you will start the raycast at a Y value which is much above the highest terrain point will be(I assume you limit the randomness some what) and start the X at the Screen.Width + some value. If you want to create mutliple coins at a time just offset the x position by a minimum distance you would like between them.

Could you please show me the code? Right know I am really stucked. I just want to spawn, let’s say 5-10 coins on the terrain and then player can pick them, after that spawn another coins after random time/distance (I don’t know if rather use generating coins after some time or if player travels some distance)

Does your terrain has collision skin? Are you raycasting in the same space as the collision skin to make sure the cast will hit it? ( same Z coord for example)?

Yes, I am. I don’t know why but some of coins are sometimes generates on the same position

But do coins always get generated where the Terrain floor is? The problem now is multiple coins end up in the same place?

As you can see it generates with a offset, inside of the collider with it collides, not on it and two on one position

hi, when you select 2 objects the editor shows only the position of the first object you selected. So what you show doesn’t mean 2 are in the same place. If you move one of the coins is there still one left behind it? That will show for sure if 2 are in the same place. As for being inside the collision, your “coins” are spheres which have their anchor point in the center of that sphere. It means that when it is positioned at the hit point of your cast, it will be inside the ground by half. When you create them at the hit point, make their position be the hit point + the coins “height”, which would allow the coin to be above the collision

Yes behind one sphere is another. Why are they are not spawning on place where the green lines (Debug.DrawRay) are?

Ah I see what you mean now about that. Is your coins parented to something? If so you can either not parent them, or you can make sure when you position them you position in world space and not local space. I think it’s a function like “transformPoint()” to transform from local to world, or inverseTransformPoint to transform from world to local.

Do you want to paste the code where you are spawning these coins at the hit point?

If you would not mind that would be great, thanks!

And how would you do the whole coin generating at the end? Make some cubes that are casting a ray like I have? Or any other better solution? Also I am struggling when to generate new coins and instantiate them just once when I need them to generate. If you would have any time to help me with that I would really appreciate that, thank you!

void SpawnCoin()
{
     Vector3 lookAhead = new Vector3(car.x + (Some number), (High Y value), car.z);
     
       RaycastHit hit;
     if(Physics.Raycast(lookAhead, Vector3.Down, out hit, 1000F))
     {
       Vector3 spawnPos = hit.point + Vector3.up * 2; //or whatever sizes coins have

       //now instaniate here
      }
}

How can I make it to spawn them next to each other instead of up on each other please?