Trouble spawning enemies out of view of the player

Hey, looking for help with some spawning info for a side-scrolling beat em up I’m working on in UE5.1

I’m trying to spawn the enemies in just slightly offscreen so they can walk in from out of the players view, to get this point I’m using Get Viewport Size, dividing it by 2, and setting that as an offset for the spawn X transform based on the x location for a spawn point trigger box (the x value centered just about where that door is behind the player)

Unfortunately what’s happening is it’s going much further to the left or right than just off screen by a pretty large margin and I’m just not sure why the viewport size seems to be so much larger than what the camera can actually see.

I did try and see if I could get the ortho view from the camera, but there doesn’t appear to be a Get Ortho View function, so that doesn’t seem like an option either.

In the picture, the space between the white boxes is what the camera can view (they’re the level bounds to stop the player from moving when locked into a battle sequence). I’d expect the enemies to spawn just to the left or right of those, but as you can see it’s much further.

viewport size is what you see right now on the screen when you play(even in editor). It changes dynamically.

Hey @Reede971!

Dejan33 is right, but also “Get Viewport Size” returns pixels, not distance, if I remember correctly.

So if your game is 1920x1080 you’re adding 1920/2= 960 to the x value. Which is pretty far, 9.6 meters.
It will continue to alter based on window size.

I think you’re thinking you’ll spawn them in a pixel-based location based on this math, but you’re converting pixels to centimeters.

Hope that helps!

Hmm I guess then i’m not quite sure what would be a route to go about this then.

I still want to make sure that i’m spawning the enemies just past the screen bounds, but what methods are available to know what those screen bounds are?

I’d still like to keep it dynamic so that it can scale with whatever screensize the user sets it to though long term i’d like to make this multiplayer firendly, so is there some other method to go about this that i’m not thinking off. I’m still newish to developement with UE, but it feels like there should be some way to to determine what is generally “off screen” for the players (once the camera is locked to the current battle)

Hello fellow devs!

Dear @Reede971, after reading your original post I got interested how to solve this. Originally I was going to do something like adding a component to character with “hard” coded offset, but then I asked AI for some help, because I realized it’s probably best to have it dynamic. I freely admit it’s not my idea, I followed AI instructions (struggled for a bit) but this is what I came up with. I don’t yet fully understand each part of the code but I’m happy to share it here. In fact, I might just move it to a fresh project without starter content and just share it, alongside the screenshot instructions from AI.

It spawns a blue and green cube out of screen with set offset (200). It does work regardless of your screen size and well as your ortho width. This is only rudimentary setup as you can only move left and right, there is no up/down movement for the character but it’s just meant to be a proof of concept.
Can you please confirm this is what you’re looking for? I will clean up the graph and might move it to fresh empty project (to reduce project size) in a bit.

Here is the end result as a video.

Below you can find the link to the project file (Zipped). It was made using Unreal Engine version 5.5.4
Spawn Actors/Enemies out of Screen (Side Scroller)

1 Like

Wow, that’s exactly what I was looking for!

Looking through the blueprint you have for it now and definitely a lot more going into it than I would have thought.

1 Like

I’m happy to hear that. I did my best to comment stuff out. Enjoy!

You did a great job with it, thinking now on how best to incorporate it and play around.

Going to be interested to see how this works when I eventually get around to the multiplayer level aspect, cause i’m assuming players will have different resolutions which makes me wonder if the viewport and thus overall are they see may be somewhat different?

But small steps for now in just getting the bones up and running, so thanks again!

Thank you!

Not sure if you would be able to directly get the view port size on runtime but maybe players could report their own resolution to the server upon joining, or you could force a resolution on players, at least until you can find a better solution?

My suggestion is to run multiple clients at the same time and see if you can get each windows viewport size on runtime.
Also I wonder if below could help out.

Yea this is the area where hopefully time, experience, and expiermentation will help win out, but i’m still just too new to know what would be the best solution there.

You figure there would be someway to assume a general “this is what everybody can view” type function or setup. Maybe I could run through the player controllers and get the largest viewport and have the enemies spawn and move to the point just inside the lowest before the AI controller takes over.

The Ortho Width seems like it would be the best route to “enforce” a view as it does seem to actually set some level of what the players can view based on that, so i’m just surprised there doesn’t seem to be a function return that value, but I guess I can just create my own by overriding stuff.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.