同步系列11-装备武器行走时的模型姿态倾斜并柔和动画的切换


想让玩家在第三人称行走时,如果角色行走方向和玩家摄像头不一致,可以产生一定的身体倾斜效果,就像现实中人急速转弯时身体会倾斜。

image-20230425104329150

我们之前已经创建了未装备武器下的混合空间1D,现在创建一下装备武器后的混合空间2D,UnEquippedIdleWalkRun里面的输入参数是Speed,然后根据Speed的移速调整到对应的动画。而EquippedRun中的输入有两个参数,一个YawOffset一个Lean。

image-20230425104632406

偏移的动画可以根据一个现有的动画修改得到,先复制一个动画的备份,调到第0帧,选中根骨骼,根据需求左转或右转5度到10度,再添加关键帧。选择创建资产-》创建动画-》当前动画-》预览网格体,然后选择要导出的路径并设置动画名称就行。一个复制的资产只能使用一次,要再修改出新的动画,需要删除并重新复制一份,不然动画帧那边会出混合的问题。

image-20230425105433406

动画部分就这么多了,接下来来看代码,都是角色动画那边的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private:
UPROPERTY(BlueprintReadOnly, Category = Anim, meta = (AllowPrivateAccess = "true"))
float YawOffset;

UPROPERTY(BlueprintReadOnly, Category = Anim, meta = (AllowPrivateAccess = "true"))
float Lean;

FRotator CharacterRotatorLastFrame;
FRotator CharacterRotator;
FRotator DeltaRotation;
//.cpp
void UBlasterAnimInstance::NativeUpdateAnimation(float DeltaTime)
{
Super::NativeUpdateAnimation(DeltaTime);

//是否装备武器
bWeaponEquipped = BlasterCharacter->IsWeaponEquipped();
bIsCrouched = BlasterCharacter->bIsCrouched;
bAiming = BlasterCharacter->IsAiming();

//角色扫射状态身体的Yaw偏移
FRotator AimRotation = BlasterCharacter->GetBaseAimRotation();//摄像机的旋转角度
FRotator MovementRotation = UKismetMathLibrary::MakeRotFromX(BlasterCharacter->GetVelocity());//角色的旋转角度
FRotator DeltaRot = UKismetMathLibrary::NormalizedDeltaRotator(MovementRotation, AimRotation);
DeltaRotation = FMath::RInterpTo(DeltaRotation, DeltaRot, DeltaTime, 5.f);//最后的插值越低动画的切换混合越柔顺不突兀
YawOffset = DeltaRotation.Yaw;//角色侧身角度

CharacterRotatorLastFrame = CharacterRotator;
CharacterRotator = BlasterCharacter->GetActorRotation();

const FRotator Delta = UKismetMathLibrary::NormalizedDeltaRotator(CharacterRotator, CharacterRotatorLastFrame);
const float Target = Delta.Yaw / DeltaTime;

const float Interp = FMath::FInterpTo(Lean, Target, DeltaTime, 6.f);
Lean = FMath::Clamp(Interp, -90.f, 90.f);//角色的倾斜角度
}

模型姿态偏移自己在第一人称是看不到的,只有第三人称和别人能看到这个效果

image-20230425110026031

角色随鼠标左右晃动的同时产生一个向左或向右的轻微偏移。


  目录