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);
    }
}
3 Likes

This is great. Thanks for sharing.

can u teach me that whts that code mean???

We get the size of the box (pressure plate) in begin play, as well as set a PressedLocation (where it would be if fully depressed)

If it’s being pressed we change the location by FInterpTo(current location, fully depressed location)

If it’s not being pressed, we change the location in the reverse direction (back to being fully unpressed).

Privacy & Terms