Unreal

[Unreal5] 특정 조건에 맞는 액터를 찾기 및 이동

usingsystem 2024. 5. 28. 22:16
728x90

Pawn 찾기

GetPawn()

현재 컨트롤러가 소유하고 있는 Pawn에 대한 참조를 가져오는 역할을 합니다.

 APawn* ControlledPawn = GetPawn();

조건에 맞는 액터 찾기

UGameplayStatics::GetActorOfClass

UGameplayStatics::GetActorOfClass는 월드 내에서 지정된 클래스의 첫 번째 인스턴스를 반환하는 함수입니다. 주로 특정 클래스의 액터가 하나만 존재하거나, 첫 번째로 발견된 액터만 필요할 때 사용됩니다.

AMyActorClass* MyActor = Cast<AMyActorClass>(UGameplayStatics::GetActorOfClass(GetWorld(), AMyActorClass::StaticClass()));
if (MyActor)
{
    // MyActor를 성공적으로 찾음
}

UGameplayStatics::GetAllActorsWithTag

UGameplayStatics::GetAllActorsWithTag는 월드 내에서 특정 태그를 가진 모든 액터를 찾아 반환하는 함수입니다. 주로 여러 액터가 동일한 태그를 가지고 있을 때 사용됩니다.

TArray<AActor*> TaggedActors;
UGameplayStatics::GetAllActorsWithTag(GetWorld(), TEXT("MyTag"), TaggedActors);
for (AActor* Actor : TaggedActors)
{
    // 각각의 Actor를 처리
}

오브젝트 이동관련

SetActorLocation

함수는 객체를 월드 좌표계에서 지정된 위치로 직접 이동시키는 함수입니다. 이 함수는 객체의 현재 위치를 무시하고 새로운 위치로 설정합니다.

함수 시그니처

bool SetActorLocation(const FVector& NewLocation, bool bSweep = false, FHitResult* OutSweepHitResult = nullptr, ETeleportType Teleport = ETeleportType::None);

 

  • NewLocation: 이동할 새로운 위치 벡터입니다.
  • bSweep: true로 설정하면 충돌을 고려하여 이동합니다.
  • OutSweepHitResult: 충돌 정보를 저장하는 선택적 매개변수입니다.
  • Teleport: 객체의 이동 방식(텔레포트 여부)을 지정합니다.

사용예시

#include "MyActor.h"

void AMyActor::MoveToLocation(FVector NewLocation)
{
    SetActorLocation(NewLocation);
}

AddActorWorldOffset

함수는 객체의 위치를 직접 변경할 때 사용됩니다. 이 함수는 지정된 오프셋만큼 객체를 이동시키며, 충돌을 고려할 수도 있고 무시할 수도 있습니다.

함수 시그니처

bool AddActorWorldOffset(FVector DeltaLocation, bool bSweep = false, FHitResult* OutSweepHitResult = nullptr, ETeleportType Teleport = ETeleportType::None);

 

  • DeltaLocation: 이동할 오프셋 벡터입니다.
  • bSweep: true로 설정하면 충돌을 고려하여 이동합니다.
  • OutSweepHitResult: 충돌 정보를 저장하는 선택적 매개변수입니다.
  • Teleport: 객체의 이동 방식(텔레포트 여부)을 지정합니다.

사용예시

#include "MyActor.h"

void AMyActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    FVector DeltaLocation(10.0f, 0.0f, 0.0f);
    AddActorWorldOffset(DeltaLocation);
}

AddMovementInput

함수는 Pawn이나 Character의 이동 입력을 처리하는 데 사용됩니다. 입력받은 결과 값을 그대로 넣을 때 사용

함수 시그니처

void AddMovementInput(FVector WorldDirection, float ScaleValue = 1.0f, bool bForce = false);
  • WorldDirection: 이동할 월드 방향 벡터입니다.
  • ScaleValue: 이동 벡터의 크기입니다.
  • bForce: true로 설정하면 Pawn이 이동할 수 없는 상황에서도 이동을 시도합니다.

사용예시

#include "MyCharacter.h"

void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
}

void AMyCharacter::MoveForward(const FInputActionValue& Value)
{
	FVector2D MovementVector = InputValue.Get<FVector2D>();

	if (MovementVector.X != 0)
	{
		//FVector Direction = FVector::ForwardVector * MovementVector.X;
		//GetPawn()->AddActorWorldOffset(Direction * 50.f);

		FRotator Rotator = GetControlRotation();
		FVector Direction = UKismetMathLibrary::GetForwardVector(FRotator(0, Rotator.Yaw, 0));
		GetPawn()->AddMovementInput(Direction, MovementVector.X);
	}
	if (MovementVector.Y != 0)
	{
		/*FVector Direction = FVector::RightVector * MovementVector.Y;
		GetPawn()->AddActorWorldOffset(Direction * 50.f);*/

		FRotator Rotator = GetControlRotation();
		FVector Direction = UKismetMathLibrary::GetForwardVector(FRotator(0, Rotator.Yaw, 0));
		GetPawn()->AddMovementInput(Direction, MovementVector.Y);
	}
}

FloationPawnMovement 추가

알아서 보간을 내부에서 진행해 주기 때문에 deltime같은 것들을 곱해주지 않아도된다.

오브젝트 회전관련

AddYawInput

APlayerController에 정의된 함수 중 하나입니다. 이 함수는 플레이어 컨트롤러의 수평 회전 값을 조정합니다.

매 프레임마다 실행되지 않고 회전값을 저장해둔다.

함수 시그니처

void APlayerController::AddYawInput(float Val);
  • Val: 회전할 양을 지정하는 부동 소수점 값입니다. 양수는 오른쪽으로 회전하고 음수는 왼쪽으로 회전합니다.

사용예시

void AR1PlayerController::Input_LookRight(const FInputActionValue& InputValue)
{
    // 마우스 오른쪽으로 이동하면 시점을 오른쪽으로 회전시킴
    float YawValue = InputValue.Get<float>();
    AddYawInput(YawValue);
}
728x90