Proper Pressure Plate

This reminded me a bit of Risk of Rain 2, so I wanted my pressure plate to, well actually show the ‘weight’ of the actor standing on it:

void APlatformTrigger::BeginPlay()
{
    Super::BeginPlay();
    if (!BoxMeshComponent)
    {
        return;
    }

    StartLocation = BoxMeshComponent->GetComponentToWorld().GetLocation();
    // z should be location - size of mesh.Z
    FVector BoxSize = BoxMeshComponent->GetComponentScale();
    float MeshZSize = BoxMeshComponent->GetStaticMesh()->GetBoundingBox().GetSize().Z;
    float MoveTo = (BoxSize.Z * MeshZSize) - ((BoxSize.Z * MeshZSize) * .10f); // give 1-10th of the box poking out
    PressedLocation = (StartLocation.Z - MoveTo);
}

// Called every frame
void APlatformTrigger::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    if (!BoxMeshComponent)
    {
        UE_LOG(LogTemp, Error, TEXT("BoxMeshComponent not set!"));
        return;
    }

    FVector CurrentLocation = BoxMeshComponent->GetComponentToWorld().GetLocation();
    if (bPressed)
    {
        CurrentLocation.Z = FMath::FInterpTo(CurrentLocation.Z, PressedLocation, DeltaTime, PlateSpeed);
        SetActorLocation(CurrentLocation);
    }
    else
    {
        CurrentLocation.Z = FMath::FInterpTo(CurrentLocation.Z, StartLocation.Z, DeltaTime, PlateSpeed);
        SetActorLocation(CurrentLocation);
    }
}
2 Likes

This is great. Thanks for sharing.