• News

• Industries

• Learning & Support

• Community

• Marketplace

# Don't spawn enemies to close to player?

I have tried so many thing and fail this one as if it were rocket science.

I have a box spawner with an array of enemies that try to keep 20 enemies in the level at all times. This is perfect but, they very often spawn right on me, or right behind me since its completely random where they spawn in the box.

How do i check for player position, design a specific radius for example around the player and avoid spawning in that radius.

I have to do this in an Actor class blueprint, which grad and resize in the environment to define the area they can spawn. All the spawn logic is in this blueprint.

Thanks in advance for any help!

I have some ideas in mind but imo the best:
When comes to spawn, you will get spawn point where should be enemy spawned.
Use node “vector length” first input your playable character location, second spawning point of enemy.
If vector length (distance between you and enemy) is < than 1000, find another spawn point (loop) until you find point in distance more than 1000 (or how mych you want)

Yeah that was logical and I’ve already had that in mind, but its getting the players location i cant seem to find a node for. This is in my spawner blueprint (actor class) from the other thread where enemy spawning is done…then i dont know how to get the info for what it chose as a random point in box before it actually spawns it.

Also if you look at this, the randoim point in bounding box goes directly into the spawn node…no iudea how to get this other info, add a >= to them and run them in that…WITH a loop involved

using a forloop, we will have floats vectors and integers all in the same spot and none of them are compatible pin connections

do it outside of function in event graph… or you need inputs…
First you need to get reference to you character.

So when you call spawn:

1. you getting random point in box - this is ok (location 1)
2. get ref to your char -> get actor location (location 2)
3. use node “vector length” with input of these locations
4. drag wire from vector length which is float and use “<” (is smaller) than 1000 for example
5. plug bool result from “<” to branch node
6. If TRUE -> call again spawn event (if spawn location is too close to your character)
7. If FALSE -> spawn actor at location from “random point in box” node

I got up to the vector length…which only has an A input and only one to go into it. I can’t add 2 locations to it! I could use 2 vector lengths but not figuring out how to tie both into the math of the 1000.
I tried a guess with a comparefloat node but that’s not going to work because i cant integrate the <1000. As in the screenshot, breaking the struct doesn’t do it either because they get turned into floats

I understand the rest

Yeah sorry, i never used that node but i knew i seen it somewhere trick is to subtract vectors

ok I tried this. it did some strange things that don’t make sense.

I got a lot more spawns, had a hug pile of dead bodies in a single small area and they spawned right next to me or behind me…and the maps are suddenly lagging quite heavily.

Are you sure its correct to subtract? and how far is 1000? I made that slot a variable so i could easily change it. I have all that stuff in functions because i have very many of these spawn calls. 2 of them are functions with the arrays connected to a flipflop so i get variation… and my 3 agents have 2 each, one where the player reacts vocally, one where he doesnt

EDIT: further playtesting shows that they are predominantly choosing the first “extra” spawner box i placed. So much even that they glitch out into the floor…i had 3 or 4 heads or more i dont know…in one single space, head above ground and still animating under the floor. Looked funny…in the pic you see the small area where a good 80% of the enemies spawn.

Yes im sure with subtract, i tested it if you want you can send me your project and i can do it. There can be many “bugs”. Absolutely the best for debugging is print string node.
Maybe if you get your character location and it returns 0 0 0, there is problem with reference.
Be sure you dont spawn enemy if branch should go to “re-call” event.
1000 distance is like 10m real units but it might look like 7m.
Or when ill be at home, i will make prototype of this and i will send you that project. Maybe in 2 hours.

OK that would be awesome, yes print string is my friend

Nothin i am trying to re-arrange is working

They spawn 90% from that exact 1 spot and in such fast succession, the second one on that spot will spawn under the floor with its head sticking out (my guess is that this is 0 0 0 and my variable distance from player is 6000 in this pic

I keep getting errors about the branch…Error Accessed None from node Branch in graph ‘EventGraph’ in blueprint EnemySpawner

I ran print tests and nothing is giving back 0’s, their location of choice to spawn, my locations nor the subtracted ones. I also ran the game with F1 on for a long time to see through everything and see how they act.

I did fix one bug so they arent spawning on me anymore unless im standing in that one spot.

in the end…i think it boils down to what i am plugging in to the location slot of the “spawn AIfrom Class” node

Man… bad news now i did many tests and it seems its really bugged… I seriously can’t find whats wrong… try to make new project, simple, 1 spawning box and logic like me… If you don’t find mistake too, let me know and imo we can post this on bug reports…

No freakin clue…i’m also getting this error:

Error Accessed None ‘CallFunc_Array_Get_Item18’ from node Spawn AIFrom Class in graph ‘EventGraph’ in blueprint EnemySpawner

I might actually have to make an array of boxes instead of getting a random of all that type in the map…ya know, define them properly.

I do however highly appreciate your effort on this one.

I heard faulty BSP block placement can cause otherwierd problems so I am going to fix that up and see if its still weird.

Why are you using bounding box . Why not use target points or player starts. Either manually place them where you want enemies to spawn or make a random algorithm to spawn them .

On game start add them all to an array . Then when you spawn simply pick a random int index from the array and spawn the enemy their . Do a check first to see how far the player is from the player start or target location if it’s so close rerun the random
Int index number and try again . Also add a timer say 5 seconds . And make sure that the timer is zero before it runs the spawn logic again to avoid enemies spawning. On top of each other or blocking the spawn point. I think what we actually did in our game was put trigger boxes on each spawn point and made an array of bools. Then checked if anything with enemy tag is overlapping or still inside trigger point then the spawn point is blocked so don’t use it

Mainly because this is a rogue-like game with various maps. you die, its over. each map will not have the same “array” and i want to avoid a ton of blueprints that are basically the same. the enemies are in arrays thats not going to change, but the amount of spawn boxes will or i just have to buckle down somehow and always use the same amount so the array works.

I never used target points or player starts…the problem with random place to spawn them is that they will spawn inside bsp walls, which is why i wanted to stop using a single bounding box for the whole map, which also forced me to keep everything on even ground.
the timer idea for respawn is fine…do you mean delay?..i’ve also not use tags so that’s gray area for me. what you are doing sounds far more solid…i guess ill be spending another month trying to figure out how to get the idea to work

I’m just at work right now . Soon as I get home I’ll create a test level showing you what I mean and hopefully it’s what you are after and can adapt it to you’re project needs

ok i FIXED it
It seems like every execution node generate like new “call” to “get random point”, so look how i stored it into variable yesterday i tried it but i did mistake and not noticed that, here we go :
BP:

BP results:

Test with 9 spawning boxes, working as expected:

edit: BP of main spawning actor (ignore “then 0” sequence, “ref to main spawning actor” is used on spawning boxes on “TRUE” branch output to “re-call” spawn event on main spawning box)

1. The box you have coming out of that for each loop…no idea what that is…if i SET to a reference of the blue print with this code in it (which seems to be what you are doing)…i get no “TARGET” pin to connect array element to.

2. if i pull off the GET in your lowest screenshot (with a custom event called start already created, it auto-breaks the GET connection and wants a reference to this blueprint instead of my spawner boxes.

3. for your getworldlocation you are using something i surely dont have…i was plugging “box” into that as well…and still am

4. Only my agents will spawn in a random box…the 2 sets of regular soldiers again have chosen a different box now to spawn in but at least now there are no errors, and using F1, they do choose a random spot in that one box so the array is NOT working.

Ill now show you my entire logic. This will read from left to right. the whole chain of logic starts from a custom event, not from event begin play.

This does nothing but keep my spawned enemy count at 20 (ignores the special agents)…once i kill one…another one should spawn. The first agent will spawn when a total of 20 have been spawned (game starts with 9 already on the map)
The flipflop there is to change between light and dark enemies…its unplugged until i get lights working correctly

This one is plugged the shot above and is basically doing most of what you did, the rest isn’t working at all and causes errors because of #1 (for loops problem with missing TARGET) It’s surely also due to this that they are only choosing one box instead of a random one from the box array

This one you should recognize easily…see #3 above for the wierdity in this part

after all that, i have a spawn counter to add too and when that’s done it runs this sequence which has all of my agent conditional spawning according to “total spawned” count. All of my agents as mentioned in #4 above.

This in game with F1 and me on a roof. They all spawn in that one box randomly (no getting stuck in the floor)…if i go there and kill them all…nothing will spawn anymore because i am close so that logic is now working…this is only about the spawners array now.

im only showing this because im not willing to change how this works in general with my spawn counting…everything on screenshot 2 and 3 can change…1 and 4 cannot!

Again thanks a great deal for your effort in this

I GOT IT DUDE!

Every bit of logic above was left as it was except for one single super small thing…that may all be very strange to you having different things on yours than mine…I’m using 4.9.2…

Have a look: