1

Currently I am using random positions outside of camera view to spawn my enemy. I want to spawn my enemies in selected areas:

Game screenshot showing area where I do not want to spawn enemies

How can I do that?

dbc
  • 104,963
  • 20
  • 228
  • 340
HitesH
  • 21
  • 1
  • 3
  • You woud need to incorporate the area excluded to your random position obtention logic, or check that if the pos is within the unwanted area, re-retrieve the position. I think the simplest way would be to define you x and y invalid ranges and for the invalid values add or substract determined distance when the pos is not valid until it is. – rustyBucketBay Dec 16 '21 at 14:44
  • Possibly related: [Instantiate objects within the terrain area](https://stackoverflow.com/q/43988991/3744182) on SO, [How do I create random game objects on a terrain, but only inside the terrain area?](https://gamedev.stackexchange.com/q/141139/158277) on [gamedev.se]. – dbc Dec 16 '21 at 14:48

2 Answers2

0

What you should do is send a raycast from your camera and check if the ray hits the area you don't want it to spawn on. If it doesn't hit it, allow it to spawn, but if it does discard the current position and try again.

Ascent
  • 86
  • 8
0

Set a Trigger collider to the area and stop the enemy spawn there

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 02:50