UE5学习笔记13-为角色添加瞄准功能

一、RBC函数的理解

        当前对于RBP函数的理解来自于现在看的视频中函数的效果,对于该函数的理解是,UE5的多人游戏中,分为服务器和客户端,当某个状态改变时会对这个状态定义一个属性标为可复制属性UPROPERTY(Reliable),可能会出现,在服务器上拿到武器后在客户端显示,但在客户端拿到武器后,服务器不显示,这时需要定义RBP函数去将客户端的变量发送到服务端,RBC函数都是声明为xxx在源文件中时xxx_Implementation,同时函数的属性是UFUNCTION(Server,Reliable),意思是服务端变量可复制(可能是-.-)---------个人理解

二、实现

        1.添加一个动作映射,项目设置->输入->操作映射->Aim(按键是鼠标右键)

        2. 在角色类中定义回调函数,绑定操作映射,函数SetupPlayerInputComponent中绑定对应回调,一个是IE_Press,一个是IE_Release

// H
void AimButtonPressed();
void AimButtonReleased();
bool IsAiming();

//CPP
bool ABlasterCharacter::IsAiming()
{
	return (Combat && Combat->bAiming);
}

void ABlasterCharacter::AimButtonPressed()
{
	if (Combat)
	{
		Combat->SetAiming(true);
	}
}

void ABlasterCharacter::AimButtonReleased()
{
	if (Combat)
	{
		Combat->SetAiming(false);
	}
}

void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	/* 绑定动作映射 */
	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);/* 具有一个输入的事件IE_Pressed */
	PlayerInputComponent->BindAction("Equip", IE_Pressed, this, &ABlasterCharacter::EquipButtonPressed);
	PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ABlasterCharacter::CrouchButtonPressed);
	PlayerInputComponent->BindAction("Aim", IE_Pressed, this, &ABlasterCharacter::AimButtonPressed);
	PlayerInputComponent->BindAction("Aim", IE_Released, this, &ABlasterCharacter::AimButtonReleased);
	/* 绑定轴映射 */
	PlayerInputComponent->BindAxis("MoveForward",this,&ABlasterCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &ABlasterCharacter::MoveRight);
	PlayerInputComponent->BindAxis("Turn", this, &ABlasterCharacter::Turn);
	PlayerInputComponent->BindAxis("LookUp", this, &ABlasterCharacter::LookUp);
}

        3.在角色动画类中

//H
UPROPERTY(BlueprintReadOnly, Category = Character, meta = (AllowPrivateAccess = "true"))/** 蓝图只读 类型是Character 允许私有访问 */
bool bAiming;

bAiming = BlasterCharacter->IsAiming();

        4.在武器组件类中GetLifetimeReplicatedProps复制bAiming变量

//H
UPROPERTY(Replicated)
bool bAiming;
void SetAiming(bool bIsAiming);

//这是一个RBP函数
UFUNCTION(Server,Reliable)
void ServerSetAiming(bool bIsAiming);

//CPP
void UCombatComponent::SetAiming(bool bIsAiming)
{
	bAiming = bIsAiming;
	//if (!Character->HasAuthority())
	//{
	//	ServerSetAiming(bIsAiming);
	//}
	ServerSetAiming(bIsAiming);
}

void UCombatComponent::ServerSetAiming_Implementation(bool bIsAiming)
{
	bAiming = bIsAiming;
}

void UCombatComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	/** 指定了具有复制变量的类 和 复制的变量是哪个  */
	DOREPLIFETIME(UCombatComponent, EquippedWeapon);
	DOREPLIFETIME(UCombatComponent, bAiming);
}

三、蓝图实现

        1.打开蓝图角色动画类中的节点,拖拽一个动画如图,所有操作如图

 

          2.对另一个节点做相同的操作最终样子如图

        3.我的动画节点只有两个上面的是蹲伏的,下面的是战力的

        4.编译保存运行调试 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值