Unreal学习之跳跃

在 Character 基类的接口文件中查看时,会发现内置的角色跳跃支持。角色跳跃与 bPressedJump 变量绑定。因此需要执行的操作是在按下跳跃动作时将该布尔型设为 true,松开跳跃动作时设为 false。 需要添加以下两个函数完成此操作:

  • StartJump

  • StopJump

在.h文件中声明跳跃函数

// 按下按键时设置跳跃标记。
UFUNCTION()
void StartJump();

// 松开按键时清除跳跃标记。
UFUNCTION()
void StopJump();

在.cpp文件中实现

// 调用后将功能绑定到输入
void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
    Super::SetupPlayerInputComponent(InputComponent);

    // 设置“移动”绑定。
    InputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
    InputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);

    // 设置“查看”绑定。
    InputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
    InputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);

    // 设置“动作”绑定。
    InputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
    InputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
}

void AFPSCharacter::MoveForward(float Value)
{
    // 明确哪个方向是“前进”,并记录玩家试图向此方向移动。
    FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
    AddMovementInput(Direction, Value);
}

void AFPSCharacter::MoveRight(float Value)
{
    // 明确哪个方向是“向右”,并记录玩家试图向此方向移动。
    FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
    AddMovementInput(Direction, Value);
}

void AFPSCharacter::StartJump()
{
    bPressedJump = true;
}

 

posted @ 2018-12-27 14:55  Cober_kuang  阅读(52)  评论(0编辑  收藏  举报