UE4 C++ Character动作映射

来源:互联网 发布:catdrawing转换cad软件 编辑:程序博客网 时间:2024/05/29 10:08

1. 首先在项目设置中,映射按键


2. 在character的头文件中,声明添加映射按键相应的函数。

其中character的基类中,已经定义了关于鼠标的旋转等相关函数(AddControllerYawInput、AddControllerPitchInput),在头文件中可以不用声明

UFUNCTION()void MoveForward(float value);UFUNCTION()void MoveRight(float value);UFUNCTION()void StartJump();UFUNCTION()void StopJump();


3. 在character源文件中,定义声明的函数

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);}//Character 基类内置的角色跳跃支持。角色跳跃与 bPressedJump 变量绑定。因此需要执行的操作是在按下跳跃动作时将该布尔型设为 true,松开跳跃动作时设为 false。void AFPSCharacter::StartJump(){    bPressedJump = true;}void AFPSCharacter::StopJump(){bPressedJump = false;}

4. 绑定映射

void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){Super::SetupPlayerInputComponent(PlayerInputComponent);//跳跃绑定InputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);InputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);// 设置“移动”绑定。InputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);InputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);InputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);InputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);}

5. 在定义移动函数时,也可按如下方式定义,看起来好理解些

void AMyThirdCharacter::MoveForward(float value)  {      if ((Controller != NULL) && (value != 0.0f))      {          // 找到当前的前方向          const FRotator Rotation = Controller->GetControlRotation();          const FRotator YawRotation(0, Rotation.Yaw, 0);                      // 获取到前方向          const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);          AddMovementInput(Direction, value);      }  }  


0 0
原创粉丝点击