人物Rotation浅析
概述
概述:
FaceRotation表明由程序自定义接管.
PhysicsRotation表明通过Lerp形式按照一定速率转向目标方向(速度方向或者Controller朝向).
并且是速度位置计算完成后, 才设置朝向.
bUseControllerRotationYaw+bUseControllerRotationPitch+bUseControllerRotationRoll,bOrientRotationToMovement,bUseControllerDesiredRotation三者互斥.- 如果同时开启,
bOrientRotationToMovement优先级高于bUseControllerDesiredRotation FaceRotation表明朝向有程序接管, 直接控制.PhysicsRotation表示根据目标朝向, 平滑转动. 目标朝向由bOrientRotationToMovement和bUseControllerDesiredRotation确定. 速率由RotationRate决定.
关键数据
UCharacterMovementComponent
bAllowPhysicsRotationDuringAnimRootMotion
1 | // 在执行RootMotion期间, 是否允许执行PhysicsRotation |
默认情况下, RootMotion期间, 不允许执行PhysicsRotation,
原因为动画直接控制人物朝向, 不需要在进行调整.

除非指定允许执行PhysicsRotation, 否则RootMotion期间,
不允许执行PhysicsRotation.

bUseControllerDesiredRotation
1 | // If true, smoothly rotate the Character toward the Controller's desired rotation (typically Controller->ControlRotation), |

如果启用该功能, 则人物朝向会平滑朝向Controller.ControlledRotation.
该字段(bUseControllerDesiredRotation)和APawn的bUseControllerRotationYaw+bUseControllerRotationPitch+bUseControllerRotationRoll是互斥的.
二者只能选其一, 否则可能会出现抖动(一般情况下不会).

当Base更新的时候, 也需要调整朝向.

bOrientRotationToMovement
1 | // If true, rotate the Character toward the direction of acceleration, |

该变量表示Character将Smooth朝着移动方向转动. 使用RotationRate作为Delta.
- base更新的时候, 刷新Rotation

- 平滑处理

RotationRate
1 | // Change in rotation per second, used when UseControllerDesiredRotation or OrientRotationToMovement are true. |
旋转速率, 根据该属性+Delta时间, 计算每帧旋转角度.


函数ComputeOrientToMovementRotation
1 | // Compute a target rotation based on current movement. Used by PhysicsRotation() when bOrientRotationToMovement is true. |
计算开启bOrientRotationToMovement后的目标朝向.
- 如果加速度不为0, 则使用加速度方向作为目标朝向.
- 如果加速度为0, 则使用速度方向作为目标朝向.
- 否则, 使用当前朝向作为目标朝向.

APawn
bUseControllerRotationYaw
1 | // If true, this Pawn's yaw will be updated to match the Controller's ControlRotation yaw, if controlled by a PlayerController. |

bUseControllerRotationPitch
1 | // If true, this Pawn's pitch will be updated to match the Controller's ControlRotation pitch, |
bUseControllerRotationRoll
1 | // If true, this Pawn's roll will be updated to match the Controller's ControlRotation roll, |
函数FaceRotation
1 | // Updates Pawn's rotation to the given rotation, assumed to be the Controller's ControlRotation. |
根据给定Rotation, 直接设置Character朝向. 即表示它的朝向受Controller的控制. 最直观的表现是它不受到速度,加速度的影响, 有Controller直接设置.

- 在DS强制设置客户端朝向的时候:
ClientSetRotation_Implementation

- 无法忽略base朝向的时候(
bIgnoreBaseRotation), 即站在可移动物体上, 要根据base朝向调整人物朝向.

- 移动合包的时候, 也需要重新设置人物朝向.

- PlayerController更新ControlledRotation时候
APlayerController::UpdateRotation

Rotation流程
关键函数UCharacterMovementComponent::PhysicsRotation.
核心:
- 根据
DeltaTime+RotationRate计算DeltaRot - 根据类型计算TargetRotation
- 计算
FixTurn

问
APawn.FaceRotation和UCharacterMovementComponent.PhysicsRotation的区别
指定: FaceRotation是指定朝向, 每帧自己算,
然后直接设置朝向. 如果需要平滑处理, 则需要自己在逻辑中添加.
平滑: PhysicsRotation是计算目标Rotation,
然后根据转速(RotationRate)平滑转过去.