同步系列1-按下shift后移速的同步


我设置的效果是按下shift降低移动速度,松开shift则恢复移动速度,你也可以根据自己的需求改成按shift奔跑,然后加个体力条,松开shift或者体力条耗尽则恢复成普通行走状态。

先看输入绑定:

1
2
3
4
//shift静步	
EnhancedInputComponent->BindAction(IA_ShiftPressed, ETriggerEvent::Triggered, this, &ABlasterCharacter::LowSpeedWalk);

EnhancedInputComponent->BindAction(IA_ShiftReleased, ETriggerEvent::Triggered, this, &ABlasterCharacter::NormalSpeedWalk);

对应于引擎中的映射事件

image-20230420181459957

先看LowSpeedWalk,这里先在本地降低角色的最大行走速度,然后也降低服务端的这名角色的最大行走速度。又因为在虚幻中角色的移动组件已经实现了网络同步,所以只要服务端的玩家速度改变了,那么就会自动同步到其他的客户端玩家上,因此这里不需要再写ClientRPC什么的。

1
2
3
4
5
6
7
8
9
10
void ABlasterCharacter::LowSpeedWalk(const FInputActionValue& InputValue)
{
GetCharacterMovement()->MaxWalkSpeed = 300.f;
ServerLowSpeedWalk();
}

void ABlasterCharacter::ServerLowSpeedWalk_Implementation()
{
GetCharacterMovement()->MaxWalkSpeed = 300.f;
}

然后是松开shift后触发NormalSpeedWalk

1
2
3
4
5
6
7
8
9
10
void ABlasterCharacter::NormalSpeedWalk(const FInputActionValue& InputValue)
{
GetCharacterMovement()->MaxWalkSpeed = 450.f;
ServerNormalSpeedWalk();
}

void ABlasterCharacter::ServerNormalSpeedWalk_Implementation()
{
GetCharacterMovement()->MaxWalkSpeed = 450.f;
}

这里松开shift后恢复速度。逻辑同上,那么到这里关于速度的同步其实就结束了,实现起来还是很简单的。


文章作者: John Doe
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 John Doe !
  目录