SpawnActor spawns in wrong location

Hello. Its my second day with Unreal, I am following tutorial on

SpawnActor spawns the actor in the same, wrong location no matter what paramaters the function gets
Basically i spawn the actor every second, and it should spawn it in random locations. I even tried hardcoding the spawn XYZ but it does not work… The spawn location seems to be (0,0,0) by the way.

FActorSpawnParameters SpawnParams;  
SpawnParams.Owner = this;  
SpawnParams.Instigator = Instigator;  
//SpawnParams.  
// Get a random location to spawn at  
FVector SpawnLocation = GetRandomPointInVolume();  
//even when i hardcoded some values it still spawned in the same wrong xyz  
/*SpawnLocation.X = -850.0f;  
SpawnLocation.Y = 0.0f;  
SpawnLocation.Z = 1000.0f;*/  
// Get a random rotation for the spawned item  
FRotator SpawnRotation;  
SpawnRotation.Yaw = FMath::FRand() * 360.f; // this should rotate the mesh, but i see the mesh in vertical position all the time  
SpawnRotation.Pitch = FMath::FRand() * 360.f;  
SpawnRotation.Roll = FMath::FRand() * 360.f;  

GetWorld()->SpawnActor(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);

What is wrong? Im using EU 4.7.6, the tutorial is pretty otudated but i dont think thats the case. Thanks in advacne for help

Hi Pancakez,

The code that you have provided (other than the hardcoded numbers that you added) look the exact same as the code provided in the video so I am expecting that the issue is elsewhere. Could you provide me with the full .cpp and .h files that you are using to spawn this object?

As a tip for posting code, you can use the ‘Code Sample’ button (Or ctrl+K on a windows keyboard) to post code while keeping syntax intact. You can also upload the files if you would like.

Thanks for reply. I actually created second, separate project with Third Person C++ starter content template. The only class added by me is Actor “SomeThing”. The spawn is bugged in exactly the same way.
Thats the code http:///sGiSgrEY

I noticed that changing RootComponent to mesh and getting rid of CollisionComponent fixes the spawn issue ( but why?? ). Everytime i press E, new, rotated brick appears falling from the sky, but the ground (the floor) doesnt stop it (i want some kind of pickup item like in that tutorial so they should stay on ground), but the character collides as i move through the falling bricks.
The code with good spawn but collision problem http:///ZE3b4EHG

I finally managed to fix that and make sphere as the collision component
The solution was to use CollisionComponent, set physics for it and not for the mesh and BaseCollisionComponent->SetCollisionProfileName(“BlockAll”);

Hi Pancakez,

It took me quite a bit of looking through your code but I believe I’ve found the issue.

In the code that you provided where the object has collision but constantly spawns at 0,0,0, you’re adding a collision component and then making that the root component and attaching the mesh to it. The collision volume is going to default to 0,0,0 and will not be changed by the function that you are calling to spawn the object.

In the second bit of code you provided you had set the Mesh as the root component, which does not have a default position so your object is spawning where intended.

I would suggest using the same code you used where the object actually collided but make the RootComponent the mesh and attach the collision component to the mesh itself.

Have a nice day

Ah, just as I post my answer, I see your edit. Good job figuring it out!

Hi,
Why would the collision volume default to 0,0,0? That doesn’t make any sense to me, the SpawnActor function should be setting the location of the actor, why is it this way?

Hello ,

I think you’re right, I was taking a look over this again (I believe this post was right after I started here actually, so I wasn’t all that familiar with the spawn functions in UE4) and it doesn’t make any sense that it would spawn at 0,0,0. Testing it out myself in 4.16, it seems to spawn at the correct location so it was either incorrect before or has been fixed in the 2 year span since this was reported.