본문 바로가기

TIL

2025 - 01 - 20 Unreal GetLocation() 값 0,0,0반환 이유 조사 (실패)

 

먼저 해당 글은 (실패)했다.

뭔가 코드 오류가 발견되어서 싱글벙글하며 블로그 작성하려고 준비했다가 

버그가 해결 "되어버렸다" 스스로 해결한 게 아니라 이건 실패다

 

그러나 또 비슷한 일이 생길 수 있으니 그럴때 현재 상황을 복습하기 위해 적어놓았다.

 

// Fill out your copyright notice in the Description page of Project Settings.


#include "NBC_Move_Actor.h"

// Sets default values
ANBC_Move_Actor::ANBC_Move_Actor()
{
 	// 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;
}

// Called when the game starts or when spawned
void ANBC_Move_Actor::BeginPlay()
{
	Super::BeginPlay();
	
	// AActor::GetWorld(); 왜 인진몰라도 AActor에서 GetWorld()가 있다
	//GetActorLocation()은 액터의 위치만 반환하는 간단한 방법입니다.
	//GetActorTransform().GetLocation()은 위치, 회전, 스케일을 포함한 더 많은 정보를 반환하는 방법
	StartLocation = GetActorLocation();

	//FVector(1,0,0) 이것도 가능
	MoveLocation = IsForward == true ? FVector::ForwardVector : FVector::RightVector;
	UE_LOG(LogTemp, Warning, TEXT("Start Location: %s"), *StartLocation.ToString());	
}

// Called every frame
void ANBC_Move_Actor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);	

	StartLocation = GetActorLocation();


	Move();

}

void ANBC_Move_Actor::Move()
{
	//GetWorld()->DeltaTimeSeconds : 해당 월드에서 프레임당 시간
	//true할시 오브젝트 닿일경우 멈춤
	AddActorLocalOffset(MoveLocation * GetWorld()->DeltaTimeSeconds * MoveSpeed);

	StartLocation = GetActorLocation();
	UE_LOG(LogTemp, Warning, TEXT("Start Location: %s"), *StartLocation.ToString());


	//FVector : 위치 방향과 관련된 함수들은 여기에 담겨 있음.
	//AActor : 액터와 관련된 함수 변수들은 여기에 담겨 있음.
	//뭔가 서로 기능이 곂쳐보이는 것 도 있겠지만 FVector는 어디에서든 가져다 사용할 수 있는 도구?
	//AActor는 해당 객체와 관련된 것만 사용할 수 있는 본체 
	if (FVector::Distance(GetActorLocation(), StartLocation) > MaxRange)
	{				
		//거리가 처음 위치에서 MaxRange 만큼 떨어졌다면 MoveSpeed를 * -1 로 해주어서 반대 방향으로 움직임
		MoveSpeed *= -1;
	}

}

 

 

// Fill out your copyright notice in the Description page of Project Settings.


#include "NBC_Move_Actor.h"

// Sets default values
ANBC_Move_Actor::ANBC_Move_Actor()
{
 	// 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;
}

// Called when the game starts or when spawned
void ANBC_Move_Actor::BeginPlay()
{
	Super::BeginPlay();
	
	// AActor::GetWorld(); 왜 인진몰라도 AActor에서 GetWorld()가 있다
	//GetActorLocation()은 액터의 위치만 반환하는 간단한 방법입니다.
	//GetActorTransform().GetLocation()은 위치, 회전, 스케일을 포함한 더 많은 정보를 반환하는 방법
	StartLocation = GetActorLocation();

	//FVector(1,0,0) 이것도 가능
	MoveLocation = IsForward == true ? FVector::ForwardVector : FVector::RightVector;
	UE_LOG(LogTemp, Warning, TEXT("Start Location: %s"), *StartLocation.ToString());	
}

// Called every frame
void ANBC_Move_Actor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);	
	
	Move();
}

void ANBC_Move_Actor::Move()
{
	//GetWorld()->DeltaTimeSeconds : 해당 월드에서 프레임당 시간
	//true할시 오브젝트 닿일경우 멈춤
	AddActorLocalOffset(MoveLocation * GetWorld()->DeltaTimeSeconds * MoveSpeed);


	//FVector : 위치 방향과 관련된 함수들은 여기에 담겨 있음.
	//AActor : 액터와 관련된 함수 변수들은 여기에 담겨 있음.
	//뭔가 서로 기능이 곂쳐보이는 것 도 있겠지만 FVector는 어디에서든 가져다 사용할 수 있는 도구?
	//AActor는 해당 객체와 관련된 것만 사용할 수 있는 본체 
	if (FVector::Distance(GetActorLocation(), StartLocation) > MaxRange)
	{				
		//거리가 처음 위치에서 MaxRange 만큼 떨어졌다면 MoveSpeed를 * -1 로 해주어서 반대 방향으로 움직임
		MoveSpeed *= -1;
	}

}

 

현재 상황 : BeginPlay에서 현재 위치를 감지해서 넣어줄 StartLocation에 현재 위치여야될(-60, 250, 50) 이아닌 (0,0,0) 이 반환되는 상황에서 Tick에다가 적용해보니 잘 작동 되는걸 확인 후

다시 버그 이유를 알아보려고 tick에서 함수를 지워봤더니 정상적 작동 실행..컴파일은 도중에 한거같은데 아직 이유는 

정확히 발견 못하였음.