Input Binding

Last updated

Last updated
// ZCCharacterMovementComponent.h
public:
void WantsClimbing();
void CancelClimbing();
private:
bool bWantsToClimb = false;// ZCCharacterMovementComponent.cpp
void UZCCharacterMovementComponent::WantsClimbing()
{
if (!bWantsToClimb)
{
bWantsToClimb = CanStartClimbing();
}
}
void UZCCharacterMovementComponent::CancelClimbing()
{
bWantsToClimb = false;
}// ZCClimbingCharacter.h
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* ClimbAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* CancelClimbAction;
protected:
void Climb(const FInputActionValue& Value);
void CancelClimb(const FInputActionValue& Value);// ZCClimbingCharacter.cpp
void AZCClimbingCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent->BindAction(ClimbAction, ETriggerEvent::Triggered, this, &AZCClimbingCharacter::Climb);
EnhancedInputComponent->BindAction(CancelClimbAction, ETriggerEvent::Triggered, this, &AZCClimbingCharacter::CancelClimb);
}
}
void AZCClimbingCharacter::Climb(const FInputActionValue& Value)
{
if (MovementComponent)
{
MovementComponent->WantsClimbing();
}
}
void AZCClimbingCharacter::CancelClimb(const FInputActionValue& Value)
{
if (MovementComponent)
{
MovementComponent->CancelClimbing();
}
}// ZCCharacterMovementComponent.cpp
void UZCCharacterMovementComponent::OnMovementUpdated(float DeltaSeconds, const FVector& OldLocation, const FVector& OldVelocity)
{
if (bWantsToClimb)
{
SetMovementMode(EMovementMode::MOVE_Custom, ECustomMovementMode::CMOVE_Climbing);
}
Super::OnMovementUpdated(DeltaSeconds, OldLocation, OldVelocity);
}