How to Properly Create Physics Volume for Fluid using C++?

In this project, I would like to spawn a physics volume in a sea using C++, as I am manually modifying sea level in the code. The code below can spawn the physics level. However, it is not working correctly, as my character is supposed to swim whenever it overlaps with the Physics Volume. (IsSwimming from NavMovementComponent = false even after the character enters the physics volume, should be true instead).

    //OceanSpawnLocation/OceanSpawnRot is a FVector value of where the ocean locates/rotates in.
    //Boxsize is the FVector size of the Ocean.
    FActorSpawnParameters PhysicsVolumeSpawnParams;
	APhysicsVolume* PhysicsVolume = GetWorld()->SpawnActor<APhysicsVolume>(APhysicsVolume::StaticClass(), OceanSpawnLocation, OceanSpawnRot, PhysicsVolumeSpawnParams);
	PhysicsVolume->SetActorScale3D(BoxSize);
	UWorld *World = GEditor->GetEditorWorldContext().World();
	UCubeBuilder* CubeBuilder = Cast<UCubeBuilder>(GEditor->FindBrushBuilder(UCubeBuilder::StaticClass()));
	CubeBuilder->X = oceanVolumelengthX;
	CubeBuilder->Y = oceanVolumeWidthY;
	CubeBuilder->Z = oceanVolumeHeightZ;
	CubeBuilder->Build(World);
	GEditor->Exec(World, TEXT("BRUSH ADDED"));

Below is the Settings of the Physics Volume while playing. If I manually add the physics volume using the same settings below without using the C++ code, the character would swim properly.

Any suggestions to resolving the issue would be appreciated!

Do you ever set it to be a water volume?

Yep, I do currently, and I am currently using these code:

	FActorSpawnParameters PhysicsVolumeSpawnParams;
	APhysicsVolume* PhysicsVolume = GetWorld()->SpawnActor<APhysicsVolume>(APhysicsVolume::StaticClass(), OceanSpawnLoc - deductionVector, OceanSpawnRot, PhysicsVolumeSpawnParams);
	UWorld *World = PhysicsVolume->GetWorld();
	UCubeBuilder* CubeBuilder = Cast<UCubeBuilder>(GEditor->FindBrushBuilder(UCubeBuilder::StaticClass()));
	CubeBuilder->X = oceanVolumelengthX;
	CubeBuilder->Y = oceanVolumeWidthY;
	CubeBuilder->Z = oceanVolumeHeightZ;
	PhysicsVolume->BrushBuilder = (UBrushBuilder *) CubeBuilder;
	PhysicsVolume->Brush = NewObject<UModel>(PhysicsVolume, NAME_None, RF_Transactional);
	PhysicsVolume->Brush->Initialize(PhysicsVolume, false);
	PhysicsVolume->BrushType = EBrushType::Brush_Add;
	PhysicsVolume->BrushBuilder->Build(World, PhysicsVolume);

	PhysicsVolume->SetNeedRebuild(PhysicsVolume->GetLevel());
	//GEditor->RebuildAlteredBSP(); For some reason, this line triggered a breakpoint, with the error: Edit and Continue : error  : 'ForestController.cpp' in 'UE4Editor-ProceduralGenerator.dll' was not compiled with Edit and Continue enabled. Ensure that the file is compiled with the Program Database for Edit and Continue (/ZI) option.

With the last commented line, the code crashes with breakpoint. Without the last line, this still yields the same result → Scaled CubeBuilder without isSwimming functionalities.

Sometimes, this whole thing also returns a weird error: BrushComponent0 has to be ‘Movable’ if you’d like to move.

Any further suggestions to resolving the issue would be appreciated!

I’ve found a workaround - To manually create a PhysicsVolume in the UE Editor, set it to movable, and then set its location and scale.

	TSubclassOf<APhysicsVolume> classToFind;
	classToFind = APhysicsVolume::StaticClass();
	TArray<AActor*> foundPhysicsVolume;
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), classToFind, foundPhysicsVolume);

	foundPhysicsVolume[0]->SetActorScale3D(BoxSize);
	foundPhysicsVolume[0]->SetActorLocation(OceanSpawnLoc - deductionVector);

Feel free to comment below if you’ve found a solution directly addressing to my problem, without manually creating a PhysicsVolume.