# How to spawn actor relative to pawn?

I’d like my pawn to spawn an actor at a certain distance away from itself. Here’s what I’m working with currently:

When pawn is possessed, spawn actor at given location, and when unpossessed, destroy the spawned actor. I want the location to be derived from the pawn’s origin * 2 on X and Y axes.

This kinda works, but there are a couple of issues.

First, the actor is spawned at incorrect and inconsistent locations. On some pawns in the level, the actor spawns closer, while on others it spawns farther away. It’s never at the actual desired location.

Lastly. while the actor spawns as it should when the pawn is possessed, for some reason it also spawns when the level is loaded. There’s no event that tells it to do so.

What am I doing wrong?

1 Like

If you want to spawn something on specified distance from pawn, you should do:

SpawnLocation = PawnLocation + PawnForwardVector*DesiredDistance

Additionally you can randomize location by some extent (like box extent)

SpawnLocation = PawnLocation + PawnForwardVectorDesiredDistance + RandomUnitVectorSpawnExtent

Something like this?

Doesn’t seem to work though. None of the pawns spawn the actor when they’re possessed.

Awesome, this works really well. Thanks!

A small issue I’ve run into, is that the spawn happens a little bit higher than it should. With this setup, the actor should spawn at the same height as the origin of the pawn’s root component, correct? But it actually spawns 2cm higher.
It’s not a big deal, and I’ve used an Up vector node to correct it. I’m just curious why it happens.

Maybe Forward vector have some precision issues? I mean it looks not like (0.5, 0.5, 0), but like (0.5, 0.5, 0.00001).

I think you can try to set its Z value to 0 with BreakVector and MakeVector functions.

I don’t think that’s it.

After experimenting for a little bit, I figured out that I don’t really need the forward vector for anything. All I need is GetActorLocation + offset amount. But even without the forward vector node, the spawn still happens 2cm higher than it should.

Unrelated question: is it possible to spawn multiple actors with the SpawnActor node, or do I have to copy paste the whole setup?

2cm higher than it should.

Maybe GetActorLocation returns some wrong value?

is it possible to spawn multiple
actors with the SpawnActor node, or do
I have to copy paste the whole setup?

You can do it in loop

Thanks for telling me about that! I’m working on an array with a bunch of different locations for the actor to spawn at. Seems to work, so I guess I’m doing it right?

As for GetActorLocation returning some wrong value, I don’t know. Maybe the issue is actually with the root component instead. I’ll look into it.

I’m more concerned with the other issue I mentioned in the OP. Why does the actor spawn when the level is loaded? It should only spawn when the pawn is possessed. I still haven’t found a solution to this.

Thanks for trying to help, but I managed to fix the issue.

As you can see in the blueprint, my pawn is supposed to spawn several actors when it’s possessed, but for some reason, the actors would already be spawned when the level was loaded.

I found that in the pawn’s class defaults, there’s a setting called Auto Possess AI, which (unsurprisingly) enables the AI to auto possess the pawn. So when loading the level, the pawn would be possessed automatically, which is why the actors were spawned.

This is probably obvious to anyone who is experienced with UE4, but as a beginner, I had no idea.

Yes, array of locations and foreachloop is ok.

Why does the actor spawn when the
level is loaded?

Can you explain more details?

I think it may work in that way, if you use OpenLevel.