文章目录
前言
区分ControllRotation和ActorRotation
- ControllerRotation表示PlayerController的方位,PlayerController和PlayerCameraMenager会在游戏开始的时候生成
- ActorRotation表示RootComponent的方位
UE4基础
UE4使用的是左手坐标系
欧拉角
欧拉角是什么,欧拉角用来描述一个物体的方位,即朝向。
例子1:
首先明确坐标轴属性,什么颜色对应什么轴,红色对应X轴的旋转量,绿色对应Y州的旋转量,蓝色对应Z轴的旋转量。
由FRotator 保存:
- Pitch:俯仰(上下看)— Y轴
- Yaw:偏航(左右看) — Z轴
- Roll:翻转(旋转)—X轴
注意:Roll-Pitch-Yaw组合(X-Y-Z轴),强烈建议记住英语单词的意思。
第二部分表达UE4的旋转功能,当前三个轴的旋转量都为0
如果顺时针转动蓝色网格时候,Z轴将会变大,如果是逆时针转动,Z轴的旋转量将会变小。
绕Z轴顺时针旋转45°的情形:
欧拉角和旋转矩阵是可以相互转换的
在本地坐标系中,我们发现X,Y,Z轴出现了方位的变化,它们的值需要通过旋转矩阵才能知道,它们值还可以通过GetForwardVector,GetRightVector和GetUpVector函数得到。
下面是绕Z轴顺时针旋转45°得到的旋转矩阵图:
关闭人物的旋转和ControlRotation一致
这个表示ControlRotation旋转,人物自身的ActorRotation也会旋转。可以在Character蓝图属性中找到
开启镜头随着鼠标移动
设置弹簧臂,我们需要让弹簧臂使用Pawn身上的ControllerRotation来转动自身,即用PlayerController控制弹簧臂
开启人物随着加速度方向旋转
在CharacterMovment中,有一个Orient Rotation to Movement,表示人物ActorRotation将会随着加速度方向而变动,旋转速率可以调节
MoveForward(ControllerRotation)
这边使用的是ControllerRotation的旋转量,表示人物移动的方向是跟着眼睛的方向走动的
人物将会朝着红色箭头方向移动,不会朝着黑色箭头移动
void AThirdPersonMPCharacter::MoveForward(float Value)
{
if ((Controller != NULL) && (Value != 0.0f))
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// 旋转矩阵中x轴的分量表示前向向量 (GetForwardVector)
//FRotationMatrix() 把欧拉角转换为旋转矩阵
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
在设计人物前进后退,左右移动时候,我们只需要关注Controller关于Z轴的旋转量,即Yaw量,其他的旋转量不应该影响人物的行动。
- 确定一个旋转矩阵中的前进方向,即EAxis::X
- 确定一个旋转矩阵中的左右方向,即EAxis::Y
- AddMovementInput改变的是位移量,即Location
错误案例:
void AThirdPersonMPCharacter::MoveForward(float Value)
{
if ((Controller != NULL) && (Value != 0.0f))
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
// 获取前向矢量
const FVector Direction = FRotationMatrix(Rotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
当我们的Controller朝上看的时候,我们发现GetForwardVector指向了天上,由于人物没有自带飞行功能或者受到重力的影响,将无法朝天上前进,这样就会导致人物原地不动,而不是向前移动,这样不符合我们的行为习惯!
MoveForward(ActorRotation)
朝着黑色箭头的方向移动
void AThirdPersonMPCharacter::MoveForward(float Value)
{
if (Value != 0.0f)
{
// add movement in that direction
AddMovementInput(GetActorForwardVector(), Value);
}
}
LookUp 和 Turn
PlayerInputComponent->BindAxis("Turn", this, &APacManCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &APacManCharacter::AddControllerPitchInput);
- Controller的上下左右视角转动改变的旋转量,即Roll-Pitch-Yaw
- AddControllerYaw/PitchInput:即改变Rotation量
知识拓展
人物蓝图中,有一个关于UseControllerRotationPitch/Yaw/Roll的属性介绍下
首先区分Controller和人物角色两个不同的方位,首先人物角色的方位在图上已经有一个坐标系表示了,但是Controller的方位是不一样,镜头的方位是朝前看的,所以当我们不使用UseControllerRotationXXX时候,Controller和人物角色是分离的,即第三人称视角,如果对UseControllerRotationYaw打上勾,表示使用第一人称视角!
void AThirdPersonMPCharacter::TurnAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
}
bUsePawnControlRotation
摄像机组件和弹簧组件都有关于bUsePawnControlRotation属性,表示这些组件是否使用Controller的旋转量,即当Controller改变了是否同时改变弹簧和摄像机的位置,可以方便制作旋转摄像头
MovementComponent
- Orient Rotation to Movement: 朝着加速度方向转向,可以实现原地打转,设置旋转速率
- Use Controller Desired Rotation:朝着照相机的方向缓慢旋转
- Ignore Base Rotation:忽略基础旋转
总结
建议用UE4第一人称和第三人称模板体验一下
原文:https://blog.csdn.net/weixin_44200074/article/details/108536398