[UE5] TIL - 21 <OnComponentHit, OnComponentHit not working>

ChangJin·2024년 4월 16일
0

Unreal Engine5

목록 보기
49/102
post-thumbnail

2024-04-15

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

느낀점
언리얼에서 사용하는 델리게이트 개념을 배울 수 있었다. 콜백함수로 함수의 주소를 전달하여 사용하는 방법도 매우 신기했다. 어떤 이벤트가 발생했을때 Broadcast한다는 개념이 신기했다.

TIL

  • OnComponentHit
  • OnComponentHit not working

OnComponentHit

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


  • 이게뭐냐하면 어떤 딱딱한 물체에 부딪치거나 혹은 내가 충격을 받거나 했을때 호출하는 이벤트이다.
  • UPrimitiveComponent에 있다.
  • sweep과 overlap을 담당하던 바로 그 친구이다

  • Projectile.cpp에서 시작과 동시에 OnHit 함수를 콜백함수로 등록해준다. 델리게이트 기능을 보고 있기때문에 만약 여기서 충돌이 발생하면 OnHit 함수를 실행시키고 이 델리게이트를 보고있는 다른 Mesh들에게 정보를 알려준다.
void AProjectile::BeginPlay()
{
	Super::BeginPlay();

	ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
}

  • 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;
}


OnComponentHit not working

https://forums.unrealengine.com/t/oncomponenthit-function-not-working/483103


  • 이거 왜안돼... 위의 BeginPlay에서 분명히 콜백함수를 등록해주었는데 발사체가 이동하지 않았다. 구글링끝에 나랑 비슷한 처지의 사람이 올린 질문글을 찾았다.

  • vscode나 vs나 raider같은 에디터를 켜놓고 해결방법은 언리얼 엔진을 종료시키고 완전 종료한 다음에 에디터에서 빌드를 해주고 성공적으로 빌드를 했으면 다시 언리얼 엔진을 켜보자. 마법같이 잘 될것이다.
profile
Unreal Engine 클라이언트 개발자

0개의 댓글