//左右旋转 视角
PlayerInputComponent->BindAxis("Turn",this,&ACharacter::AddControllerYawInput);
//上下旋转 视角
PlayerInputComponent->BindAxis("LookUp",this,&ACharacter::AddControllerPitchInput);
//X轴 围绕X轴旋转 &ACharacter::AddControllerRollInput //Y轴 围绕Y轴旋转 &ACharacter::AddControllerPitchInput //Z轴 围绕Z轴旋转 &ACharacter::AddControllerYawInput向移动的方向旋转 .h文件里声明 移动函数
//前后移动 void MoveForward(float Value); //左右移动 void MoveRight(float Value);
绑定移动函数
//前后移动
PlayerInputComponent->BindAxis("MoveForward",this,&AMyCharacter::MoveForward);
//左右移动
PlayerInputComponent->BindAxis("MoveRight",this,&AMyCharacter::MoveRight);
需要修改 移动的方法
//前后移动
void AMyCharacter::MoveForward(float Value)
{
if(Controller != nullptr && Value != 0)
{
//得到控制器的旋转
FRotator Rotation = Controller->GetControlRotation();
//围绕Z轴旋转
FRotator YawRotation(0.0f,Rotation.Yaw,0.0f);
//得到这个矩阵的单位长度轴 得到这个矩阵的单位长度轴
FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
//向哪个方向移动
AddMovementInput(Direction,Value);
}
}
//左右移动
void AMyCharacter::MoveRight(float Value)
{
if(Controller != nullptr && Value != 0)
{
//得到控制器的旋转
FRotator Rotation = Controller->GetControlRotation();
//围绕Z轴的旋转
FRotator YawRotation(0.0f,Rotation.Yaw,0.0f);
//得到这个矩阵的单位长度轴
FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
//向哪个方向移动
AddMovementInput(Direction,Value);
}
}
在构造方法 添加如下代码
//如果为真,这个Pawn的滚动将被更新以匹配控制器的ControlRotation滚动(如果被PlayerController控制的话) bUseControllerRotationRoll = false; //x bUseControllerRotationPitch = false; //y bUseControllerRotationYaw = false; //z //旋转朝向移动 //如果为真,将角色旋转到加速的方向,使用RotationRate作为旋转变化的速率。 //覆盖UseControllerDesiredRotation。通常,您需要确保清除其他设置,例如字符上的bUseControllerRotationYaw。 GetCharacterMovement()->bOrientRotationToMovement = true; //每秒旋转的变化量,当usecontrolerdesiredrotation或OrientRotationToMovement为真时使用。 //为无限旋转速率和瞬时旋转设置一个负值。 GetCharacterMovement()->RotationRate = FRotator(0.0f,500.0f,0.0f);蓝图里需要 把这几个参数 设置一下



