Implementation:
ADoor : public AActor, public IInteractable- All interactables override the
OnInteract()function
void ADoor::OnInteract_Implementation(APawn* Pawn)
{
if (Pawn && !GetIsLocked())
{
const FVector Impulse = Pawn->GetActorForwardVector();
PredictRotation(Impulse);
}
}- When a
APawninteracts with with or collides with the door, we attempt to Predict where the door should be:
void ADoor::PredictRotation(const FVector& ImpulseDir)
{
if (!Door) return;
// Finds what side of the door is being hit.
const double angle = FVector::DotProduct(ImpulseDir, Door->GetForwardVector());
// Front of door
if (angle < 0.0)
{
//UKismetSystemLibrary::PrintString(this, "Forward", true, false, FLinearColor::Black, 100.0f, "Direction");
const float x = -PushForce + CurYaw;
TargetDoorYaw = FMath::Max(x, -InteriorSwingDistance);
}
// Back of door
else if (angle > 0.0)
{
//UKismetSystemLibrary::PrintString(this, "Backward", true, false, FLinearColor::Black, 100.0f, "Direction");
const float x = PushForce + CurYaw;
TargetDoorYaw = FMath::Min(x, ExteriorSwingDistance);
}
// Side of door
else
{
return;
}
CurYaw = TargetDoorYaw;
bIsRotating = true;
}- Finally, the
TargetYawgets replicated, & all clients have the door interpolate from theCurrentYawto theTargetYaw.
DOREPLIFETIME(ADoor, TargetDoorYaw);
Leave a Reply