Mass Scale of Physics Mesh, Dynamically Update During Runtime

From Epic Wiki
Jump to: navigation, search

Dear Community,

Please note that in UE4, physics objects / Kactors are actually Static Mesh Actors that are set to have physics simulation active.

So the code below is for an AStaticMeshActor deriving class of your own making.

Here is a function you can use to set the mass scale of a Static Mesh Actor that is using physics simulation, DURING runtime!


Yes, you can dynamically update the mass of UE4 Physics Objects anytime you want during runtime!


The key thing to note is the function

UpdateMassProperties()

Which is present in BodyInstance.h

void AYourStaticMeshActorClass::SetMassScale(const float& NewScale)
{
	if(!StaticMeshComponent) return;
       //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	FBodyInstance* BodyInst = StaticMeshComponent->GetBodyInstance();
	
	if(!BodyInst) return;
	//~~~~~~~~~~~~~~~~~~~~~~~~
	
	// New Scale 
	BodyInst->MassScale = NewScale;  
	
	// Trigger Update! 
	BodyInst->UpdateMassProperties();
}


Summary

By accessing the BodyInstance of a Static Mesh Actor you can update its mass scale during runtime,

making physics objects in your game world lighter or heavier any time you want :)

Enjoy!

Rama (talk)