[UE5] TIL - 20 <UProjectileMovementComponent, UPrimitiveComponent>

ChangJin·2024년 4월 16일
0

Unreal Engine5

목록 보기
48/102
post-thumbnail

2024-04-13

깃허브!
https://github.com/ChangJin-Lee/ARproject
https://github.com/ChangJin-Lee/ToonTank

느낀점
우선 UPrimitiveComponent는 해당 컴포넌트의 tick동안에 다른 컴포넌트의 위치를 업데이트한다. 발사체에 대한 아주 자세한 설정이 가능하다. UPrimitiveComponent는 되게 다양한 정보를 가지고 있는데, 특히 collision data와 관련된 geometry 종류들이 아주 많이 들어있다. 그리고 이건 SceneComponents이다. 발사체와 관련된 내용을 배우면서 굉장히 흥미로웠다. tick 마다 좌표값을 더해서 움직을 주는것이 아니라 특정 컴포넌트를 가져다가 사용하는 점이 매우 인상깊었다.

TIL

  • UProjectileMovementComponent
  • UPrimitiveComponent

UProjectileMovementComponent

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Engine/GameFramework/UProjectileMovementComponent?application_version=5.3


  • 발사체에 움직임을 주기위해 사용한다.
  • tick 마다 다른 컴포넌트의 위치를 업데이트할 수 있다.
  • 즉 캐릭터가 발사한 발사체의 위치를 업데이트할 수 있다는 이야기이다!

  • 다음처럼 Projectile.h에 선언해주고
	UPROPERTY(VisibleAnywhere, Category = "Movement")
	class UProjectileMovementComponent* ProjectileMovement;

	// 콜백함수가 작동하려면 UFUNCTION이어야만 함
	UFUNCTION()
	void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);```

  • Projectile.cpp에 다음처럼 디폴트 값을 만들어준다.
  • ProjectileInitialSpeed와 ProjectileMaxSpeed는 짐작하는 것처럼 헤더파일에서 만들어 주면 된다.
// Sets default values
AProjectile::AProjectile()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));
	RootComponent = ProjectileMesh;

	ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
	ProjectileMovement->InitialSpeed = ProjectileInitialSpeed;
	ProjectileMovement->MaxSpeed = ProjectileMaxSpeed;
}


UPrimitiveComponent

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Engine/Components/UPrimitiveComponent?application_version=5.3


  • 발사체의 움직임을 만들어주었으니 이제 발사체가 부딪힌 Component 정보를 가져와야한다. 그래야 적마다 데미지를 다르게 한다던지, 벽에 부딪힌것인지 등의 구분이 가능하다.

  • 위의 Projectile.h에서 선언해둔 OnHit을 Projectile.cpp에서 완성시키자.

void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	auto MyOwner = GetOwner();
	if(MyOwner==nullptr) return;

	auto MyOwnerInstigator = MyOwner->GetInstigatorController();
	auto DamageTypeClass = UDamageType::StaticClass();
	// UDamageType을 블루프린트 기반으로 만들거나 별도 데이터를 설정하는게 아니기 때문에
	// TSubclassOf 변수는 필요 없다.

	if(OtherActor && OtherActor != this && OtherActor != MyOwner)
	{
		UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, this, DamageTypeClass);
		Destroy();
	}
}

  • 이제 발사체를 파괴할 수 있다.
  • 앞으로 남은 것은 플레이어와 적의 HP, UI, 각종 편의기능 구현이다!
profile
Unreal Engine 클라이언트 개발자

0개의 댓글