[6번 과제] 회전 발판과 움직이는 장애물 퍼즐 스테이지
회전 발판, 이동 플랫폼 외에 추가적인 액터 (예: 톱니바퀴, 엘리베이터, 트랩 등)을 직접 발상해서 구현했는가? - 라는 평가 기준도 있어 일정 시간마다 사라졌다 나타나는 발판도 만들었습니다.
이동 발판
#include "MovingPlatform.h"
AMovingPlatform::AMovingPlatform()
{
PrimaryActorTick.bCanEverTick = true;
MovingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MovingMesh"));
RootComponent = MovingMesh;
}
void AMovingPlatform::BeginPlay()
{
Super::BeginPlay();
//현위치에서 x,y축 기준으로 일정 범위내로 랜덤하게 스폰
StartLocation = GetActorLocation();
FVector RandomLocation = StartLocation + FVector(
FMath::RandRange(-RandomRange, RandomRange),
FMath::RandRange(-RandomRange, RandomRange),
0.0f
);
SetActorLocation(RandomLocation);
}
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector CurrentLocation = GetActorLocation();
float DistanceMoved = FVector::Dist(StartLocation, CurrentLocation);
if (DistanceMoved >= MaxRange)
{
bMovingForward = !bMovingForward;
}
float Direction = bMovingForward ? 1.0f : -1.0f;
AddActorLocalOffset(FVector(Direction * MoveSpeed * DeltaTime, 0, 0));
}
제 프로젝트에서의 회전 발판은 트랩으로 튕겨나가도록 기획해 랜덤하게 스폰이 이상해 이동 발판만 랜덤하게 스폰하도록 구현했습니다.
회전 발판
#include "RotatingTrap.h"
ARotatingTrap::ARotatingTrap()
{
PrimaryActorTick.bCanEverTick = true;
RotatingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RotatingMesh"));
RootComponent = RotatingMesh;
}
void ARotatingTrap::BeginPlay()
{
Super::BeginPlay();
OriginalRotation = RotatingMesh->GetRelativeRotation();
GetWorld()->GetTimerManager().SetTimer(RotationTimerHandle, this, &ARotatingTrap::ToggleRotation, RotationInterval, true);
}
void ARotatingTrap::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bIsRotating)
{
//Yaw축 기준으로 회전
FRotator NewRotation = FRotator(0.0f, RotationSpeed * DeltaTime, 0.0f);
RotatingMesh->AddLocalRotation(NewRotation);
}
else
{
RotatingMesh->SetRelativeRotation(OriginalRotation);
}
}
void ARotatingTrap::ToggleRotation()
{
bIsRotating = !bIsRotating;
}
사라지는/나타나는 발판
#include "BlinkingPlatform.h"
ABlinkingPlatform::ABlinkingPlatform()
{
PrimaryActorTick.bCanEverTick = true;
BlinkingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlatformMesh"));
RootComponent = BlinkingMesh;
}
void ABlinkingPlatform::BeginPlay()
{
Super::BeginPlay();
SetVisibility();
GetWorld()->GetTimerManager().SetTimer(BlinkTimerHandle, this, &ABlinkingPlatform::ToggleVisibility, BlinkInterval, true);
}
void ABlinkingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ABlinkingPlatform::ToggleVisibility()
{
bIsVisible = !bIsVisible;
SetVisibility();
}
void ABlinkingPlatform::SetVisibility()
{
BlinkingMesh->SetVisibility(bIsVisible);
BlinkingMesh->SetCollisionEnabled(bIsVisible ? ECollisionEnabled::QueryAndPhysics : ECollisionEnabled::NoCollision);
}
그 외에도 캐릭터가 낙사하도록 구현했습니다.
void AHW6Character::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (GetActorLocation().Z < -500.0f)
{
RespawnPlayer();
}
}
void AHW6Character::RespawnPlayer()
{
APlayerController* PlayerController = Cast<APlayerController>(Controller);
if (PlayerController)
{
AActor* PlayerStart = UGameplayStatics::GetActorOfClass(GetWorld(), APlayerStart::StaticClass());
if (PlayerStart)
{
FVector SpawnLocation = PlayerStart->GetActorLocation();
FRotator SpawnRotation = PlayerStart->GetActorRotation();
SetActorLocationAndRotation(SpawnLocation, SpawnRotation);
}
}
}
'내일배움캠프 > TIL' 카테고리의 다른 글
[내일배움캠프 Day28] 7주차 과제 진행 (1) | 2025.01.24 |
---|---|
[내일배움캠프 Day27] 플레이어 이동 처리하기 (1) | 2025.01.23 |
[내일배움캠프 Day25] 액터의 라이프 사이클 (2) | 2025.01.21 |
[내일배움캠프 Day24] 개발 환경 설정 (1) | 2025.01.20 |
[내일배움캠프 Day23] CH2 팀 프로젝트 (3) | 2025.01.17 |