Make an object spawn outside the screen?

Hi !
I’m making enemies, and I just want to know how to make them spawn outside the camera ?

Hi, you can use the CameraWidth() and/or CameraHeight() expression and then combine it with values for creating your enemies outside the camera.

1 Like

Did you want to create an enemy at a random spot just outside the camera, anywhere in a border around the window?

I needed to do that once. What I ended up doing was creating a “border” sprite object to represent the area where an enemy can spawn. I put four of them in an “interface” layer that does not follow the camera (so that the border object always sits just outside the camera) so that they surround the camera.

Then, in my events I used “pick a random object” event to pick one of the border sprite objects, and create a sprite with x position = x position of border object + random( border object.width). The random function used with the width property of the border object will place the x somewhere within the width of the box of the border object

Then do a similar thing for y position (new enemy y = border y + random( border height)

1 Like

You’re a genius !
I created an invisible box that which takes up the whole screen and I made enemies move away this box when they spawn, but your solution is better.

Hello! I am trying to spawn enemies off of the screen using the technique you’re talking about here. I made a bunch of spawn points all around the camera window (on their own interface layer) and I am picking a random spawn point to spawn a random enemy. It seems that the problem is that the spawn point x/ys don’t seem to be moving with the actual spawn points! Though I can verify that they are staying stationary around the screen, the original points seem to be where the enemies keep coming from, not the actual position of the spawn points.

I made a workaround where I give the spawn points the same movement as the player sprite but when I hit a collision and stop, they keep travelling at their set speed and get out of whack again.

What am I missing?

Any help would be greatly appreciated!

Here is the event I’m using:

Keep in mind each layer has a unique camera, but doesn’t have unique coordinates. Meaning if you have spawn points on the interface layer that doesn’t move the camera, the spawn points don’t actually match up to the visual position of your main game layer.

Not sure on Luxon5s scenario. But what you are experiencing is what should be happening with how you set it up.

Thanks for the info! How do I change the situation? Make a camera on the interface layer as well?

It depends on what you want to do.

Do you just want to spawn “outside of the screen” at random points? Or specific points outside of the screen?

For “outside of the screen”, I just pick a random direction (up/down/left/right) and spawn at a randomized location above the top/bottom/left/right boundary of the screen.

I’ve been working on a “Vampire Survivors” example template, and this is how I spawn enemies (only 100 enemies at a time in Vampire Survivors, you won’t need the repeat event type, and should instead use whatever conditions you want before spawning)

For “Specific points”, just use similar expressions, but instead of “RandomInRange” Just add whatever amount you need. Let’s say you want to spawn enemies at 50 pixels above the topmost border of the screen, you’d use “CameraBorderTop(“yourgamelayerhere”,0)-50” (Y decreases when going visually up, so you subtract 50 pixels). If you have multiple points above the top of the screen, you’d just randomly pick one using the same variable method I do in the screenshot above.

But if you’re wanting vampire survivor style spawning, my screenshot is exactly how that works.


Wow! This is exactly what I’m wanting to do! I’m also working on a survivors-like. Would it be possible for you to send me the example template so I could grab events directly from it? In the random in range, what are the numbers there? The two-thousands? Does that have to do with the camera area resolution?

I actually don’t know where that project template is, otherwise I’d share it. The screenshot is an old one I have on the discord.

My example template was 1080p/1920x1080. The numbers are there to ensure the enemies spawn somewhere far way (nearly 1 screen distance away) to as close as slightly outside of view (~150 pixels). This was to ensure you never saw the spawning itself happen.

Got ya. If you ever do find that template, I’d LOVE to see it. I’m going to try to set up the event like you have it above. I think that will be perfect. Thanks so much!

Keep in mind, the “Repeat 100 times” above is just to start the level. My understanding is you only spawn more enemies up to enemy count of 200 (onscreen or not), and despawn most (nonboss) enemies if they are X amount away from the camera.

So the above events would be start of scene, then you’d have some sort of timer that checks if the enemies (put all of your enemy objects into a group) total count is <200, then spawn until 200 with similar events (Just without the repeat 100 times), then reset the timer once enemies => 200.

One final question for now, which event spawns in which direction, 0 - 3? My game resolution is 640 wide by 360 high. Just want to know which numbers to feed in where.

Whichever one you want. The random just selects an event below. Just keep in mind if you want it to be above the screen you’re wanting one with a Y position that is negative. Below the screen you want a Y position that is positive, to the left you want an X position that’s negative, to the right you want an X position that’s positive.

I tried to type out the action like you have it above and the editor says that there are multiple problems with what I put in. Are there any variables in that event that I need to set up first?

SpawnDir would be important, but you don’t have to preset up anything. Just make sure you’re typing things as they appear, everything is case sensitive.

(also in the screenshot above, it’s 0 = Top, 1 = left, 2 = bottom, 3 = right)

1 Like

This is what I typed in:


Edit: nevermind, that’s the forum using special formatting. :smiley:

Also, no idea what (layer:“”) is for at the end, but it definitely shouldn’t be there.

Here’s what it says when I put in that code:

Need to see the whole event to give you any context. Also your “CameraBorderTop” is missing the “”,0, but it shouldn’t matter as mine was too.