Apply Collision When Spawning A Pawn

Hi!

I am creating a simple blueprint script to spawn a pawn at the place of mouse when left mouse button is pressed.

This is the first time I create something in UE without following tutorials and I’ve never missed with the physics system before.

The problem is when I try to spawn actors near each other they go into each other.

In Unity, it was as easy as adding colliders, but I tried this here and it doesn’t work.

Any ideas?

You could offset the location by the bounds.

Here’s a basic check if bounds are colliding. You could also perform a check for distance and side and calculate the offset.

Xintoc has a good solution.

Another option (depending on your gameplay intent) could be to offset the spawning of your actor vertically (Z axes) from the ground a ways. If you enabled physics on the actor it would fall to the ground and collide with other actors. This would cause your objects to pile up rather than interpenetrate. You can play around with the physics and collision options in your blueprint to change the way the actor’s collision/physics behaves if you want to do things like adjust its weight or clamp an axes so that it’s doesn’t move a certain direction when it falls.

Is there any way to detect if they will collide before actually spawning them?
Like strategy games, I want to prevent player from spawning an actor very close to the other.

You can’t detect something that doesn’t exist, only predict. You already know the size of the bounds or the object that is being spawned. Work with that. If you are going to have lots of uniform objects you can work out a grid system.

I mean that in games like Command and Conquer and Starcraft, when you choose a building, a slightly faded model for it appears and allows you to place it. If you place it on another building, it gives you error. Can I do this?

How do you think that is achieved?

Going with the RTS style system, here is how I’d go about trying to solve it.

first off, I’d say the majority of RTS games don’t have you drag an actor around and place the EXACT actor. What devs are doing is basically a magic trick. They have an “testing” actor that represents the desired mesh that the user can move around the world. This “testing” actor is simply doing collision tests on other actors. If, at any moment the testing actor reports back that it is not colliding with any other actors they the user can hit a button and spawn the real mesh/blueprint actor at that location. If the testing actor reports back that it is colliding with something, then the user can’t use the place functionality/swap.

So, to accomplish this affect, (note, I’m just riffing here. I’ve never attempted this myself) I would create a testing cube. In that cube blueprint I would make sure it had an invisible collision box around it. In its event graph I would use that collision box to trigger overlap on and off events. I would then create a boolean variable that would be “CanPlace” or something like that.

I’d then set the boolean variable via the overlap events to the boolean variable. OverlapOn = False (can’t place) OverlapOff = True.

The next thing I’d do is in player character, mess around with creating and moving your new testing BP. Sounds like you’ve got a handle on that so I won’t go into detail. basically with this functionality you will spawn your testing cube in the world and move it around. The testing cube will be constantly testing to see if it’s getting overlap events.

The final pice would be to complete the magic trick by spawning the real actor BP. To do this, you’d probably add something to your player character that would use a Branch to test if the testing cube’s boolean is true or false. So, you’d have an input that would place your cube. That input would cast to the testing cube and grab the boolean’s value. If the boolean is true (i.e. CanPlace is true), then you pass along and spawn the real actor BP at the location.

You’ll want to make sure that your real actor BP also has a collision box for the testing actor to test against.

Hope this gives you some good direction to play with.

Oh, and to get fancy, you could open up your testing BP and continue off your overlap events to change the material of the testing cube so that you have a visual indicator. Make the default material green, switch the material to red if it’s overlapping. switch it back to green if it overlaps off. Good luck!

One final thing. Make sure your testing BP cube is the same size as the cube you’re placing. Obviously if you were spawning a more detailed mesh, you’d use that same mesh in your testing BP instead of a cube. Magic!

OK, I’ve missed a bit with Unity to test some ideas (I am way much better at C# than this Blueprints).

What I have done so far is that I made two objects one is a prefab (blueprint) and the other is a regular gameobject (actor) in the scene.

I’ve created the PlayerController script and in it, I made a raycast (linetrace) from the camera towards the mouse and get the hit point (until now I don’t know how to get it in Unreal as location and impact point both return the location of the actor).

I made the fadedActor (the regular one) move to the hit point (as to move with the mouse pointer) without affection its Z location.

What I am trying to achieve now is check if the fadedActor is colliding with another actor and if true, disable spawning.

I finally got it in Unity in less than half the time I spent in Unreal with no result.

I made all the steps mentioned above, but with adding a rigidbody for collision detection and everything is all right!

Can I make such a thing in Unreal? I am actually considering using Unity again.

All of this is easy except having the fadedActor (or magic actor as you call it) follow the mouse.

If you read my post below, you’ll find that I’ve done something a bit similar to this in Unity.

I followed what you did but it is not working.

Can you tell me what’s wrong with this? It is not spawning anything.

There is also a stutter when the FadedActor moves.

haven’t watched it, but this video may shed some light on what you are wanting to do.

Too many posts for now, so this is my final one :slight_smile:

I finally managed to get it to work.

Here are my until-now-working blueprints should anyone need them.