게임개발/Unreal C++

[Unreal]#9-2_Shooting 설계 및 구현

Hardii2 2022. 8. 20. 14:45

[Unreal]#9-2_Shooting 설계 및 구현

Unreal 개발 중 "Gun Actor"에 대해 알아보겠습니다.

"Simple Shooter Game"의 Character Class 개발 과정 중 일부입니다.

 

 


 

Impact Effect 

 

1. UParticleSystem* 타입의 변수를 데이터 멤버로 선언합니다.

 

class AGun : public AActor
{
//...

private:
    UPROPERTY(EditAnywhere)
    UParticleSystem* ImpactEffect;
};

 

1. Hit 위치에 Impact 이펙트를 재생합니다.

 

if(bSuccess == true)
{
    FVector ShotDirection = -Rotation.Vector();
    UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, hit.Location, ShotDirection.Rotation() );
}

 

* SpawnEmitterAtLocation() ?
UParticleSystemComponent* UGameplayStatics::SpawnEmitterAtLocation(
    const UObject* WorldContextObject, 
    UParticleSystem* EmitterTemplate, 
    FVector SpawnLocation, 
    FRotator SpawnRotation, 
    FVector SpawnScale, 
    bool bAutoDestroy, 
    EPSCPoolMethod PoolingMethod, 
    bool bAutoActivateSystem
    )
: 특정 Effect를 주어진 위치와 회전에 재생합니다. 해당 Effect가 플레이가 완료된 후에 사라집니다.

 

* FHitResult 구조체?

: 하나의 Trace를 통해 발생한 Hit에 대한 정보를 담고 있습니다.
 

FHitResult

Structure containing information about one hit of a trace, such as point of impact and surface normal at that point.

docs.unrealengine.com

 

3. 적절한 Impact Effect를 UPROPERTY를 통해 할당

 

 

 


 

 

Damage 구현

 

1. AActor::TakeDamage() 호출

 

if(bSuccess == true)
{
    FVector ShotDirection = -Rotation.Vector();
    UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, hit.Location, ShotDirection.Rotation() );
    
    if(hit.GetActor() != nullptr)
    {
    	// FPointDamageEvent는 FDamageEvent의 하위 클래스입니다.
    	FPointDamageEvent DamageEvent(Damage, hit, ShotDirection, nullptr);
    	hit.GetActor()->TakeDamage(Damage, DamageEvent, OwnerController, this);
    }
}

 

* AActor::TakeDamage() ?
float AActor::TakeDamage(
        float DamageAmount,
    	FDamageEvent const& DamageEvent, 
    	AController* EventInstigator, 	// Damage Causer의 Controller를 의미, eg) 나
    	AActor* DamageCauser		// 직접 Damage를 입힌 Actor를 의미 eg) Gun
    )
: Actor 클래스에서 제공하는 메서드로, Actor에게 Damage를 적용합니다.

 

* FDamageEvent 클래스?
: AActor::TakeDamage()와 관련 함수들에 활용되는 Event를 정의합니다.