Console Command
런타임 중에 디버깅용 PrintString() 함수 호출이나
디버깅용 드로잉을 자유자재로 끄고 키고자 하는 경우에
콘솔 커맨드를 활용하면 된다
언리얼 에디터 > 틸드키(`)를 통해 콘솔 커맨드 입력이 가능하다
Console Command 생성
우리만의 콘솔 커맨드를 만들기 위해서는
아래의 코드와 같이 작성하면 되고
콘솔 커맨드에 "StudyProject.ShowAttackDebug 1"을 작성해서
콘솔 변수에 1을 넣어 활용할 수 있다
// SPlayerCharacter.h
...
class STUDYPROJECT_API ASPlayerCharacter : public ASCharacter
{
...
public:
static int32 ShowAttackDebug;
protected:
...
};
// SPlayerCharacter.cpp
...
int32 ASPlayerCharacter::ShowAttackDebug = 0;
FAutoConsoleVariableRef CVarShowAttackDebug(
TEXT("StudyProject.ShowAttackDebug"),
ASPlayerCharacter::ShowAttackDebug,
TEXT(""),
ECVF_Cheat
);
ASPlayerCharacter::ASPlayerCharacter()
{
...
}
...
디버그 드로잉
충돌을 체크하는 것을 시각적으로 보기위해서
언리얼에서 제공하는 디버그 드로잉이라는 기능을 이용하면 된다
DrawDebugCapsule()
캡슐 모양을 그려주는 함수이다
캡슐의 반지름을 설정하고, 탐색 시작 위치와 끝 위치로 향하는 벡터,
그 벡터의 중심 위치와 벡터 길이의 절반을 인자로 작성하면 된다
기본값으로 캡슐은 상하로 서있기 때문에
쿼터니언 회전 행렬을 적용해서 캐릭터 시선 방향으로 눕혀야 한다
// SPlayerCharacter.cpp
...
void ASPlayerCharacter::OnCheckHit()
{
FHitResult HitResult;
FCollisionQueryParams Params(NAME_None, false, this);
bool bResult = GetWorld()->SweepSingleByChannel(
HitResult,
GetActorLocation(),
GetActorLocation() + MeleeAttackRange * GetActorForwardVector(),
FQuat::Identity,
ECC_GameTraceChannel2,
FCollisionShape::MakeSphere(MeleeAttackRadius),
Params
);
/*
if (true == bResult)
{
if (true == ::IsValid(HitResult.GetActor()))
{
UKismetSystemLibrary::PrintString(this, FString::Printf(TEXT("Hit Actor Name: %s"), *HitResult.GetActor()->GetName()));
}
}
*/
#pragma region CollisionDebugDrawing
if (ShowAttackDebug == 1)
{
FVector TraceVector = MeleeAttackRange * GetActorForwardVector();
FVector Center = GetActorLocation() + TraceVector + GetActorUpVector() * 40.f;
float HalfHeight = MeleeAttackRange * 0.5f + MeleeAttackRadius;
FQuat CapsuleRot = FRotationMatrix::MakeFromZ(TraceVector).ToQuat();
FColor DrawColor = true == bResult ? FColor::Green : FColor::Red;
float DebugLifeTime = 5.f;
DrawDebugCapsule(
GetWorld(),
Center,
HalfHeight,
MeleeAttackRadius,
CapsuleRot,
DrawColor,
false,
DebugLifeTime
);
if (true == bResult)
{
if (true == ::IsValid(HitResult.GetActor()))
{
UKismetSystemLibrary::PrintString(this, FString::Printf(TEXT("Hit Actor Name: %s"), *HitResult.GetActor()->GetName()));
}
}
}
#pragma endregion
}
...
'게임 엔진 > [코드조선] 언리얼' 카테고리의 다른 글
언리얼 AI - AIController (0) | 2024.05.16 |
---|---|
언리얼 Damage Framework - TakeDamage 함수 (0) | 2024.05.15 |
언리얼 Collision - 충돌 감지 (0) | 2024.05.15 |
언리얼 Collision - 충돌 설정 (0) | 2024.05.15 |
언리얼 Animation - Animation Notify (0) | 2024.05.14 |