Could I get some help with physics issues with Respawnable objects?

I’m working on respawning items affected by physics, like weapons lying on the ground.

When the item is collected I do this:

WorldCollision->SetSimulatePhysics(false);
SetActorEnableCollision(false);
SetActorHiddenInGame(true);

Then when it respawns I do this:

WorldCollision->SetSimulatePhysics(true);
SetActorTransform(InitialTransform);
SetActorEnableCollision(true);
SetActorHiddenInGame(false);

It works pretty well most of the time. But if the player is overlapping with the item when it respawns, the physics don’t work.

My WorldCollisionMesh is set to ignore Pawns, so I can’t even imagine why this would happen. I have a separate collision sphere for colliding with Pawns and letting the item get collected.

Also when I change the respawn code to reenable physics afterwards instead of before:

SetActorTransform(InitialTransform);
SetActorEnableCollision(true);
SetActorHiddenInGame(false);
WorldCollision->SetSimulatePhysics(true);

The object appears at it’s InitialTransform position for a frame then immediately snaps to where it would normally be as if the physics got simulated at a very fast rate. And if the player is standing over it, it falls through the floor.

#Wake All Bodies

I encountered a similar issue with going in and out of my game mode in my in-game editor, with an object that should only simulate physics during game time and be still in the editor mode

for the INITIAL setting of physics, you dont have to wake all rigid bodies

but for subsequent calls to setting physics to active you have to manually trigger all bodies to be awakened

WorldCollision->SetSimulatePhysics(true);
WorldCollision->WakeAllRigidBodies();

SetActorTransform(InitialTransform);
SetActorEnableCollision(true);
SetActorHiddenInGame(false);

#Solution

Make your own inline function that always calls both, when you are activating physics, and you are set :slight_smile:

Rama

Thanks. I wasn’t even aware of those functions.

I found out the real problem and have it posted as my answer.

I found the issue. The order I was doing this was incorrect.

SetActorEnableCollision(true);
SetActorHiddenInGame(false);

If I enable collision first, then the OnOverlap method gets called again, making the item get picked up. So now the item is gone again.

But then the next line gets executed, making the item visible, when it actually should be invisible due to it having been just picked up.

You have to do it in this order:

SetActorHiddenInGame(false);
SetActorEnableCollision(true);

Make the actor hidden first, then enable collision. That way, if the player is standing right over the item, it won’t be reset back to visible upon being recollected.

I was totally mistaking this for the physics being frozen somehow earlier.