How to set min and max flying height

Hi, I’m trying to get my player to fly up and down on the z axis, however when it hits the minimum or maximum position it stops moving. How can I get it to work with my constraints.

My function looks like:
void UCustomMovementComponent::MoveUp(float AxisValue)
{

	if (GetOwner()->GetActorLocation().Z > MIN && GetOwner()->GetActorLocation().Z < MAX)
	{
		FVector DeltaLocation = FVector(0.0f, 0.0f, AxisValue * MoveSpeed * GetWorld()->DeltaTimeSeconds);
		GetOwner()->AddActorLocalOffset(DeltaLocation, true);
	}
}

Thank you

Hi KielanT

You could just call GetOwner()->AddActorLocalOffset() outside the if check, that way the delta location will only update whilst the condition is true, otherwise, it will use the last valid value.

Another way would be to use FMath::Clamp which will lock a value between a min and max range and remove the IF check altogether.

> void AMyPawn::Tick(float DeltaTime) {
> 	Super::Tick(DeltaTime);
> 
> 	float AxisValue = /* Logic for axis
> value */
> 
> 	FVector DeltaLocation = FVector(0.0f,
> 0.0f, FMath::Clamp(AxisValue, MIN, MAX)  * MoveSpeed *
> GetWorld()->DeltaTimeSeconds);
> 	 
> 	GetOwner()->AddActorLocalOffset(DeltaLocation,
> true);
> 
> }

Hope this helps.

Good luck

Alex

Thank you, however moving the local offset out of the if statement doesn’t work. Then the clamp causes it to just fly up because the Axis value is used for the input so its either 1, 0 or -1.

So I figured out, it’s not pretty but it works

if (AxisValue < 0 && GetOwner()->GetActorLocation().Z >MIN)
	{
		FVector DeltaLocation = FVector(0.0f, 0.0f, -1.0f * MoveSpeed * GetWorld()->DeltaTimeSeconds);
		GetOwner()->AddActorLocalOffset(DeltaLocation, true);
	}
	else if (AxisValue > 0 && GetOwner()->GetActorLocation().Z < MAX)
	{
		FVector DeltaLocation = FVector(0.0f, 0.0f, 1.0f * MoveSpeed * GetWorld()->DeltaTimeSeconds);
		GetOwner()->AddActorLocalOffset(DeltaLocation, true);
	}