UE5/UE5 MultiPlayerGame

MultiPlayerGame) 20. 줍기 시스템

PJNull 2022. 6. 22.
728x90
반응형

줍기 시스템

무기가 있더라도 아이템을 획득하지 못한다면 무기가 아니라 그냥 배경과 다르지 않을 것이다. 이번 포스팅에서는 아이템을 줍는것을 구현하고자 한다.

 

 

1.줍기 위젯 생성

 

먼저 유저인터페이스->위젯블루프린트를 생성하고 텍스트를 추가 해준다.

 

위젯 블루프린트 생성

 

텍스트 추가

 

다음으로 C++에도 추가해준다.

//Weapon.h

private:

	UPROPERTY(VisibleAnywhere, Category = "Weapon")
		class UWidgetComponent* PickUpWidget;

 

//Weapon.cpp

#include "Components/WidgetComponent.h"



AWeapon::AWeapon()
{
	PickUpWidget= CreateDefaultSubobject<UWidgetComponent>(TEXT("PickUPWidget"));
	PickUpWidget->SetupAttachment(RootComponent);
}

 

무기 블루프린트에 추가한 위젯을 설정해준다.

 

 

 

2. 콜리젼 오버랩 설정

 

보통 게임에서는 캐릭터가 물체와 접촉을 하였을때 위젯이 뜨는 시스템으로 되어있다. 이것을 구현하고자 한다.

 

먼저 만들어 두었던 구체와 오버랩할수 있는 함수를 불러온다.그리고 OnComponentEndOverlap에 바인딩 해준다.

 

//Weapon.h


protected:
	UFUNCTION()
	virtual void OnSphereOverlap
	(
		UPrimitiveComponent* OverlappedComponent,
		AActor* OtherActor,
		UPrimitiveComponent* OtherCom,
		int32 OtherBodyIndex,
		bool bFromSweep,
		const FHitResult& SweepResult
	);

 

 

//Weapon.cpp

#include "BlasterCharacter.h"

void AWeapon::BeginPlay()
{
	Super::BeginPlay();
	
	if (GetLocalRole() == ENetRole::ROLE_Authority)
	{
		AreaSphere->OnComponentBeginOverlap.AddDynamic(this,&AWeapon::OnSphereOverlap);
		AreaSphere->OnComponentEndOverlap.AddDynamic(this,&AWeapon::OnSphereEndOverlap);
	}
	
}


void AWeapon::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherCom, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	ABlasterCharacter* PlayerCharacter = Cast<ABlasterCharacter>(OtherActor);
	if (PlayerCharacter && PickUpWidget)
	{
		PlayerCharacter->SetVisibility(true);
	}

}

 

하지만 이런식으로 설정해주면 GetLocalRole() == ENetRole::ROLE_Authority에 의해서 서버는 위젯이 보이지만 Client들은 보이지 않는 상황이 발생한다.

 

 

728x90
반응형

댓글