SceneComponent를 상속받은 스크립트 US_WeaponProjectileComponent를 생성하여 작성했습니다.
입력 연결
void UUS_WeaponProjectileComponent::BeginPlay()
{
Super::BeginPlay();
const ACharacter* Character = Cast<ACharacter>(GetOwner());
if (!Character)
return;
if (const APlayerController* PlayerController = Cast<APlayerController>(Character->GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(WeaponMappingContext, 1);
}
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerController->InputComponent))
{
EnhancedInputComponent->BindAction(ThrowAction, ETriggerEvent::Triggered, this, &UUS_WeaponProjectileComponent::Throw);
}
}
}
해당 코드는 이전 글에 작성된 부분과 다른 점이 적기 때문에 간략히 살펴보려고 한다.
Subsystem->AddMappingContext(WeaponMappingContext, 1): 매핑된 입력 액션을 가져온다.
EnhancedInputComponent->BindAction(): 트리거를 통해 바인딩한다.
액션
void UUS_WeaponProjectileComponent::Throw_Server_Implementation()
{
if (ProjectileClass)
{
const auto Character = Cast<AUS_Character>(GetOwner());
const auto ProjectileSpawnLocation = GetComponentLocation();
const auto ProjectileSpawnRotation = GetComponentRotation();
auto ProjectileSpawnParams = FActorSpawnParameters();
ProjectileSpawnParams.Owner = GetOwner();
ProjectileSpawnParams.Instigator = Character;
GetWorld()->SpawnActor<AUS_BaseWeaponProjectile>(ProjectileClass, ProjectileSpawnLocation, ProjectileSpawnRotation, ProjectileSpawnParams);
}
}
ProjectileSpawnParams = FActorSpawnParameters(): 언리얼 엔진에서 액터를 동적으로 생성할 때 추가적인 설정을 제공하기 위해 사용하는 구조체이다. 지금은 소유자 설정과 스폰할 액터의 주체를 정했다.
무기 변경
void UUS_WeaponProjectileComponent::SetProjectileClass(TSubclassOf<class AUS_BaseWeaponProjectile> NewProjectileClass)
{
ProjectileClass = NewProjectileClass;
}
'Unreal > 언리얼 엔진 5로 개발하는 멀티플레이어 게임(Book)' 카테고리의 다른 글
[Unreal] 애니메이션 연결(달리기, 던지기) (0) | 2025.04.05 |
---|---|
[Unreal] 애니메이션 에셋 생성 (0) | 2025.04.04 |
[Unreal] US_BaseWeaponProjectile.cpp 코드 분석 (0) | 2025.04.02 |
[Unreal] US_Minion.cpp (체력) (0) | 2025.04.02 |
[Unreal] 경보 시스템 코드 분석 (0) | 2025.04.01 |