우선 액터 하나를 만들어준다.
그다음 액터에 beginoverlap에서 접촉판정을 내리면 될듯하다.
void ATrap::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Blue, TEXT("OverlapBegin"));
}
ARunner* OtherCharacter = Cast<ARunner>(OtherActor);
if (OtherCharacter)
{
// 두 액터(이 경우, 본인과 겹친 캐릭터) 사이의 방향을 계산하고, 위쪽 성분 추가
FVector LaunchDirection = OtherCharacter->GetActorLocation() - GetActorLocation();
LaunchDirection.Z += 10.0f; // 약간 위로 튕기게 함
LaunchDirection.Normalize();
// 튕김 강도 (필요에 따라 조절)
float LaunchStrength = 2000.0f;
// 캐릭터에 튕김을 적용
OtherCharacter->LaunchCharacter(LaunchDirection * LaunchStrength, true, true);
}
}
참고로 onoverlapbegin은 델리게이트로 바인딩해줘야한다.
아무튼 코드를 보면
디버깅용으로 로그를 찍게 했고
LaunchDirection에 트랩과 캐릭터 사이의 방향을 계산한다.
그리고 나는 살짝 위로 튕겨났으면 하기에 벡터의 z축에 값을 추가로 넣어준다.
그 뒤 해당 벡터를 정규화해준다. 그다음 LaunchStrength를 곱해서 캐릭터에 적용해준다.
(기초적인 벡터계산이므로 자세한 설명은 생략)
된다. 다만 문제가 있는데
GetActorLocation을 했을때 나오는 위치는 해당 액터의 원점이기때문에 캐릭터가 트랩의 정중앙에서 overlap되면 상승만 한다.
이런식으로 중앙에 가까울수록 더 하늘높이 날아간다.
따라서 항상 Direction벡터를 캐릭터와 트랩의 차이가 아니라 캐릭터의 무브먼트만을 이용해서 작성해보자
void ATrap::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Blue, TEXT("OverlapBegin"));
}
ARunner* OtherCharacter = Cast<ARunner>(OtherActor);
if (OtherCharacter)
{
UCharacterMovementComponent* MoveComp = OtherCharacter->GetCharacterMovement();
FVector CurrentVelocity = MoveComp->Velocity;
// 상승튕김추가
CurrentVelocity.Z += 10.0f; // 원하는 값으로 조절
CurrentVelocity.X *= -1.f;
CurrentVelocity.Y *= -1.f;
CurrentVelocity.Normalize();
float LaunchStrength = 2000.0f;
// 캐릭터에 적용
OtherCharacter->LaunchCharacter(CurrentVelocity*LaunchStrength, true, true);
}
}
오히려 더 이상해져버렸다.
무브먼트에 영향을 받기 때문에 입력이 없는채로 트랩에 닿으면 아무일도 일어나지 않는다.
무브먼트를 사용하기 보다는 다른 방법을 생각해야할듯 하다.
트랩을 4방면으로 나눠서 각각의 공간에 닿을때 같은 크기로 밀어내면 된다.
void ATrap::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Blue, TEXT("OverlapBegin"));
}
ARunner* OtherCharacter = Cast<ARunner>(OtherActor);
if (OtherCharacter)
{
// 두 액터(이 경우, 본인과 겹친 캐릭터) 사이의 방향을 계산하고, 위쪽 성분 추가
FVector LaunchDirection = OtherCharacter->GetActorLocation() - GetActorLocation();
LaunchDirection.Z += 10.0f; // 약간 위로 튕기게 함
if (GEngine)
{
FString DebugMessage = FString::Printf(TEXT("X : %f Y : %f Z : %f"), LaunchDirection.X, LaunchDirection.Y, LaunchDirection.Z);
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Blue, DebugMessage);
}
if (LaunchDirection.X > 0)
{
LaunchDirection.X = 100.f;
}
else
{
LaunchDirection.X = -100.f;
}
if (LaunchDirection.Y > 0)
{
LaunchDirection.Y = 100.f;
}
else
{
LaunchDirection.Y = -100.f;
}
LaunchDirection.Normalize();
// 튕김 강도 (필요에 따라 조절)
float LaunchStrength = 2000.0f;
// 캐릭터에 튕김을 적용
OtherCharacter->LaunchCharacter(LaunchDirection * LaunchStrength, true, true);
}
}
작동한다.
동일한 사이즈로 밀어낸다.
물론 이경우 X좌표,Y 좌표 0에 닿은경우 -방향으로 날아가겠지만 이정도는 감안할만한 오차다.
코드 리팩토링하고 마무리하면 될듯하다.
//트랩액터해더파일
UCLASS()
class TALES_API ATrap : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATrap();
virtual void Tick(float DeltaTime) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UStaticMeshComponent* TrapComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
class UBoxComponent* CollisionBox;
UFUNCTION()
void OnOverlapBegin(
UPrimitiveComponent* OverlappedComp,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult
);
private:
UPROPERTY(EditAnywhere,Category="Physics Size")
float ZSize=50.f;
UPROPERTY(EditAnywhere, Category = "Physics Size")
float XYSize=100.f;
UPROPERTY(EditAnywhere, Category = "Physics Size")
float LaunchStrength = 2000.0f;
};
헤더파일
// Sets default values
//트랩액터 cpp파일
ATrap::ATrap()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
TrapComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("TrapMeshComponent"));
RootComponent = TrapComponent;
CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBoxComponent"));
CollisionBox->SetupAttachment(RootComponent);
CollisionBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
CollisionBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
CollisionBox->SetGenerateOverlapEvents(true);
CollisionBox->OnComponentBeginOverlap.AddDynamic(this, &ThisClass::OnOverlapBegin);
}
// Called when the game starts or when spawned
void ATrap::BeginPlay()
{
Super::BeginPlay();
}
void ATrap::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Blue, TEXT("OverlapBegin"));
}
ARunner* OtherCharacter = Cast<ARunner>(OtherActor);
if (OtherCharacter)
{
// 두 액터(이 경우, 본인과 겹친 캐릭터) 사이의 방향을 계산하고, 위쪽 성분 추가
FVector LaunchDirection = OtherCharacter->GetActorLocation() - GetActorLocation();
LaunchDirection.Z = ZSize; // 약간 위로 튕기게 함
LaunchDirection.X = LaunchDirection.X > 0 ? XYSize : -XYSize;
LaunchDirection.Y = LaunchDirection.Y > 0 ? XYSize : -XYSize;
LaunchDirection.Normalize();
// 캐릭터에 튕김을 적용
OtherCharacter->LaunchCharacter(LaunchDirection * LaunchStrength, true, true);
}
}
// Called every frame
void ATrap::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
cpp파일
결과
사실 중앙으로 튕겨나는게 없어서 살짝 아쉽다.
다만 여기서 좀만 더 수정하면 될듯하니 일단 이정도만 만들고 마무리한뒤 나중에 다시 고치자
'IT > UE5' 카테고리의 다른 글
[UE5] 스턴상태일때 좌우연타시 스턴시간 감소 구현 [튕겨나는 발판 - 3] (0) | 2025.03.21 |
---|---|
[UE5] 함정을 밟으면 일정 시간동안 스턴상태 [튕겨나는 발판-2] (0) | 2025.03.21 |
[UE5] 착지 시점 감지 [2단점프구현 - 2] (0) | 2025.03.21 |
LaunchCharacter와 AddImpulse차이 (0) | 2025.03.21 |
[UE5] 2단 점프를 구현해보자 (0) | 2025.03.21 |