Hello,
I’m using the Water plugin (water ocean body with waves), and I’m trying to figure out how to make my character switch to swimming mode without having to add Physics Volumes with IsWater everywhere. So far I overridden the IsInWater in my custom character movement component:
bool UEdenCharacterMovementComponent::IsInWater() const
{
ACharacter* OwnerCharacter = Cast<ACharacter>(GetOwner());
if (!OwnerCharacter)
{
return Super::IsInWater();
}
if (!BuoyancyComponent)
{
return Super::IsInWater();
}
FVector WaterPlaneLocation;
FVector WaterPlaneNormal;
FVector WaterSurfacePosition;
float WaterDepth = 0.f;
int32 WaterBodyIdx = INDEX_NONE;
FVector WaterVelocity;
// Query buoyancy for last water surface info
BuoyancyComponent->GetLastWaterSurfaceInfo(
WaterPlaneLocation,
WaterPlaneNormal,
WaterSurfacePosition,
WaterDepth,
WaterBodyIdx,
WaterVelocity
);
bool bIsInWater = WaterDepth > 0.f;
return bIsInWater;
}
and calling it in TickCompoent:
void UEdenCharacterMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
bool bCurrentlyInWater = IsInWater();
if (bCurrentlyInWater && MovementMode != MOVE_Swimming)
{
SetMovementMode(MOVE_Swimming);
}
else if (!bCurrentlyInWater && MovementMode == MOVE_Swimming)
{
SetMovementMode(MOVE_Walking);
}
}
And I’m now looking at overriding PhysSwimming to make my char follow waves. But before that, I’m stuck with my MovementMode oscillating between Swimming and Falling as shown by those logs:
Display LogEdenCharacter MovementModeChanged: 4
Display LogEdenCharacter MovementModeChanged: 3
Display LogEdenCharacter WaterDepth=3068.37
Display LogEdenCharacter IsInWater=1
Display LogEdenCharacter MovementModeChanged: 4
Display LogEdenCharacter MovementModeChanged: 3
Display LogEdenCharacter WaterDepth=3067.23
Display LogEdenCharacter IsInWater=1
Display LogEdenCharacter MovementModeChanged: 4
Display LogEdenCharacter MovementModeChanged: 3
Display LogEdenCharacter WaterDepth=3066.13
Display LogEdenCharacter IsInWater=1
Display LogEdenCharacter MovementModeChanged: 4
Display LogEdenCharacter MovementModeChanged: 3
Display LogEdenCharacter WaterDepth=3064.94
Display LogEdenCharacter IsInWater=1
Display LogEdenCharacter MovementModeChanged: 4
Display LogEdenCharacter MovementModeChanged: 3
Display LogEdenCharacter WaterDepth=3063.67
Display LogEdenCharacter IsInWater=1
Display LogEdenCharacter MovementModeChanged: 4