Problem spawning packages

Im trying to instantiate packages at random places. Here’s what Ive done.
My roads are made from small road sprites. Ive made an array out of roads and referenced it in my script.
Now I randomly choose a road sprite from this array and get its transform.
To this transform I randomly add any value ranging from (-width, +width) and (-height, +height). And it works perfectly

Now here’s the problem. Sometimes the package would spawn under a rock (obstacle). How do i prevent this.


It’s great to see that you are challenging yourself. What you described makes sense so far.

  • What exactly do you mean by “under a rock”? And what exactly is supposed to happen? Do you want to spawn the package on top of the rock or is the package not supposed to be spawned at the position of the rock?

  • What’s the purpose of “(-width, +width) and (-height, +height)”?

Without seeing anything, I’m not sure if I understood your current implementation correctly. Do you have an array with all available spawn positions for your packages? If so and if packages appear at the wrong position, check your array. Maybe there is a wrong Transform object assigned to it.

The rocks are the obstacles which I have placed on top (higher layer) of the road sprites. I dont want the package to spawn at the position of rocks because then the player would never be able to get it.

I used the (-width/2, +width/2) and (-height/2, +height/2) because if I just use the transform component of the road sprite then the package would always spawn at the centre of the sprite. I want it to spawn anywhere inside of the road sprite and not only at the pivot point.

The packages appear at the right position. Its just that sometimes the position of rock and package coincide because the rocks are also on the roads and I dont want that to happen

Yes, in that case, your offset makes sense. I assume you ensured that the package does not get spawned on an obstacle because of the offset.

Regarding the positions, theoretically, you could “forget” the sprites and define positions that are independent from any sprites. With the OnDrawGizmos method, you could even visualise those positions for you (not the player).

Its just that sometimes the position of rock and package coincide because the rocks are also on the roads and I dont want that to happen

How did the rock get on the road? Do you spawn the rocks at random positions or do you know in advance where the rock is? If the latter, remove the position of the rock/road from your array. If the former, your code needs to handle a second array where it keeps track of “allowed” spawn positions.

Depending on how you implemented your idea and how it is supposed to work, a dictionary with all available positions (Vector3) could make sense. They would act as the key. The value could be a Transform. If the value is null, the position is free. If the value is not null, the position is occupied by “something”.

As you can see, there are multiple ways to approach your original problem. To solve your remaining problem, you have to organise your data and define rules how your program is supposed to handle certain cases.

Its the latter case. I know where the rocks are beforehand as its a fixed map. How should I approach the problem then?

From what I understood, you have an array with sprites or transforms. And the problem is, that your packages spawn at the position of a rock. Given I understood your implementation correctly, the rock sprite/transform must be part of your array because, otherwise, the packages could not be spawned at the position of the rock. The solution is to remove the rock from your array.

So what I should do is let the package instantiate. Then I’ll check its position with an array of rocks tp see if it coincides and if it does, i’ll instantiate another one. Wouldn’t this make the game slow to run? As im thinking of running a loop to check if the position of rock and package is same and there are a lot of rocks

I think what @Nina meant was that you have a list of roads and a list of rocks, so in the beginning you’ll remove the roads from the list that’s at the same position as the rocks. Your package spawner will then never pick a road that has a rock on it because they don’t exist in the list

public GameObject soulPrefab;
    [SerializeField]GameObject[] roads;
    public void spawnSoul()
        int randomNum = Random.Range(0, roads.Length);
        Vector3 loc = roads[randomNum].transform.position;
        GameObject soul = Instantiate(soulPrefab, loc, Quaternion.identity) as GameObject;

Here’s the code. Currently ive removed some functionality so the package (called soul) only spawns at the pivot point of the road but here’s the basic logic. I call this function at the start of the collision script and whenever the package gets destroyed. How do I proceed from here

Your road list should just exclude roads that have rocks on them. The rest is fine

The rock doesnt cover the entire road sprite. Its very small compared to the road. Plus there are a lot of rocks. Doing this would limit the availanle space for the packages by alot

I think the best would be to draw your idea with a pen on a sheet of paper. Ignore Unity and forget C# for a moment. Think about a solution for the problem on your paper. What is your idea? Are there any exceptions? Any rules?

Be as precise as possible when writing down your solution. You are almost there. And if you wrote it down, try to translate it into simple instructions. Once you have those instructions, the translation into C# is fairly simple because you already have everything you need.

Privacy & Terms