[내일배움캠프 Day26] C++ 6주차 과제 진행

2025. 1. 22. 21:36·내일배움캠프/TIL

[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
'내일배움캠프/TIL' 카테고리의 다른 글
  • [내일배움캠프 Day28] 7주차 과제 진행
  • [내일배움캠프 Day27] 플레이어 이동 처리하기
  • [내일배움캠프 Day25] 액터의 라이프 사이클
  • [내일배움캠프 Day24] 개발 환경 설정
개발자 밍
개발자 밍
dev0404 님의 블로그 입니다.
  • 개발자 밍
    Developer
    개발자 밍
  • 전체
    오늘
    어제
    • 분류 전체보기 (88)
      • 강의 (8)
        • UE Climbing System (3)
        • UE Dungeon (1)
        • HCI (4)
      • 책 (18)
        • 객체지향의 사실과 오해 (5)
        • Effective C++ (3)
        • 이득우의 게임 수학 (4)
        • 이것이 취업을 위한 컴퓨터 과학이다 (4)
        • 리뷰 (2)
      • C++ (2)
      • 알고리즘 (2)
      • 자료구조 (1)
      • Unreal (4)
      • 내일배움캠프 (52)
        • TIL (52)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    그래픽스
    객체지향
    컴퓨터구조
    내일배움캠프
    알고리즘
    컴퓨터 구조
    Effective
    언리얼
    자료구조
    c++
    게임수학
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
개발자 밍
[내일배움캠프 Day26] C++ 6주차 과제 진행
상단으로

티스토리툴바