No collision on StaticMesh built at Runtime on Packaged version

Hello !

I’m creating a StaticMesh at runtime, but there’s no collision at all in packaged mode, and I’m quite stuck (it works fine in Editor). All given solutions to similar problems I found didn’t work for me.

Here is what I’m doing :

  • I have an AActor placed in my scene for the terrain/landscape, with a default flat “prebuilt” plane StaticMesh attached its StaticMeshComponent. I’m enabling Collision for Query only (what I want), and Block on wanted CollisionResponseChannel. This one works fine even in Packaged version.
ATerrain::ATerrain()
{	
    //...
    DefaultTerrainStaticMesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/TerrainLoader/SM_DefaultTerrain.SM_DefaultTerrain'")).Object;
    //...
}

void ATerrain::BeginPlay()
{
    //...
    SMComponent = static_cast<UStaticMeshComponent*>(GetComponentByClass(UStaticMeshComponent::StaticClass()));
	SMComponent->SetCollisionEnabled(ECollisionEnabled::Query);
	SMComponent->SetCollisionResponseToChannel(GROUND_OBJECT_TYPE_CHANNEL, ECR_Block);
    ResetTerrain();
    //...
}

void ATerrain::ResetTerrain() const
{
    SMComponent->SetStaticMesh(DefaultTerrainStaticMesh);
}
  • I load some files and create a new StaticMesh based on data in it for a new Terrain
    → For this, I utilize FMeshDescription, FMeshDescriptionBuilder, FBuildMeshDescriptionsParams and other few tools, since I build my StaticMesh vertex per vertex. (I hope I didn’t cut something important)
TOptional<UStaticMesh*> FTerrainHelper::GenerateMesh(...) const
{
	//...
	FMeshDescription MeshDesc;
	FStaticMeshAttributes Attributes(MeshDesc);
	Attributes.Register();
	
	FMeshDescriptionBuilder MeshDescBuilder;
	MeshDescBuilder.SetMeshDescription( &MeshDesc );

	//... Vertices and triangles computing

	UStaticMesh* StaticMesh = NewObject<UStaticMesh>();
	
	UStaticMesh::FBuildMeshDescriptionsParams MdParams;
	MdParams.bBuildSimpleCollision = true;
	MdParams.bFastBuild = true;
	
	TArray<const FMeshDescription*> MeshDescriptions;
	MeshDescriptions.Emplace(&MeshDesc);
	StaticMesh ->BuildFromMeshDescriptions(MeshDescriptions, MdParams);
	
	return StaticMesh;
}
  • Once I created the new StaticMesh, I set it on my already existing StaticMeshComponent
void ATerrain::GenerateTerrain(...) const
{
	TOptional<UStaticMesh*> StaticMesh = TerrainHelper->GenerateMesh(...);

	if(StaticMesh.IsSet())
	{
		SMComponent->SetStaticMesh(StaticMesh.GetValue());
	}
	//...
}

… and it doesn’t work on packaged version. No collision ever is detected on my newly created Terrain (I click on it to place points or objects, and no reaction). But it works perfectly fine in Editor :confused:
I have the solution to use a DynamicMesh instead, but those are quite expensive and the Terrain may be… big. And since I don’t need to edit it once it’s created, StaticMesh would be the way to go I think.

Someone has an idea regarding how to fix this ? Or what the Editor is doing and I need to “reproduce” ?

Thanks :o

Did you solve this problem? I also encountered the same problem

I kinda solved it, but not in the way I wanted.
I finally used a DynamicMesh (with a custom FMeshShapeGenerator) instead, but it’s not as expensive as a thought (at least in my use case). I even use both at the same time, because I have a default terrain that is static, and switch between them if I reset my terrain to default.

I suspect something not being recomputed at runtime (like my Material problem in another post I made), but is in Editor. Since I found a decent solution for me, I didn’t dig in the engine source code again this time.
And even if it was the case, there should be a reason that this recomputing thing isn’t being done at runtime.