본문 바로가기

강동새싹언리얼수업/언리얼

NPC 배회기능 적추적기능 C++

배회기능 라인트레이스에서 충돌이 감지되면 오른쪽으로 돈다.

무한 공간에서는 직진거리를 제한할 필요가 있다.

void AMyPlayer::Move(float DeltaTime) {
	FHitResult _HitOut;
	FVector _Start = GetActorLocation();
	FVector _End = GetActorLocation() + (GetActorForwardVector() * traceDistance);
	FCollisionQueryParams _TraceParams;
	GetWorld()->LineTraceSingleByChannel(_HitOut, _Start, _End, ECC_Visibility, _TraceParams);

	DrawDebugLine(GetWorld(), _Start, _End, FColor::Magenta, false, 3.0f);
	//AMyPlayer *_player = Cast<AMyPlayer>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
	auto *_player = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);

	FVector _pLocation = _HitOut.ImpactPoint;
	FVector _myLocation = GetActorLocation();
	FVector _newDir = _pLocation - _myLocation;
	float _targetDistance = _newDir.Length();

	SetActorRotation(_newDir.Rotation()); //orientRotation을 켜주면 안넣어줘도 된다.
	// get forward vector
	FVector Direction = FRotationMatrix(_newDir.Rotation()).GetUnitAxis(EAxis::X);
	if (_HitOut.GetActor()) {
		// 월드에서, AI 폰의 현재 좌표를 기준으로 6미터 이내 16개의 각을 가진 빨간색 원 0.2초간 생성
		DrawDebugSphere(GetWorld(), _HitOut.ImpactPoint, 10.f, 16, FColor::Magenta, false, 1.0f);
		bool isCharacter = (Cast<ACharacter>(_HitOut.GetActor())== nullptr);
		GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("Debug %f %s %d"),
			_newDir.Length(), *_HitOut.GetActor()->GetName(),isCharacter));
		_targetDistance = (_HitOut.ImpactPoint - _myLocation).Length();
	}

	if (_targetDistance < 200.f) {
		//AddMovementInput(FVector::Zero());
		_pLocation = GetActorLocation()+GetActorRightVector()*100.f;
		_newDir = _pLocation - _myLocation;
		_targetDistance = _newDir.Length();
		SetActorRotation(_newDir.Rotation()); //orientRotation을 켜주면 안넣어줘도 된다.
		Direction = FRotationMatrix(_newDir.Rotation()).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction.GetSafeNormal() * DeltaTime * 10.f);
	}
	else {
		AddMovementInput(Direction.GetSafeNormal()* DeltaTime * 10.f);
	}


}

적 추적기능 정리필요 적 일정거리내에 도달하면 멈춤

void AMyPlayer::Trace(float DeltaTime)
{
	FHitResult _HitOut;
	FVector _Start = GetActorLocation();
	FVector _End = GetActorLocation() + (GetActorForwardVector() * traceDistance);
	FCollisionQueryParams _TraceParams;
	GetWorld()->LineTraceSingleByChannel(_HitOut, _Start, _End, ECC_Visibility, _TraceParams);

	DrawDebugLine(GetWorld(), _Start, _End, FColor::Magenta, false, 3.0f);
	//AMyPlayer *_player = Cast<AMyPlayer>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
	auto* _player = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);

	FVector _pLocation = _player->GetActorLocation();
	FVector _myLocation = GetActorLocation();
	const FVector _newDir = _pLocation - _myLocation;
	float _targetDistance = _newDir.Length();

	SetActorRotation(_newDir.Rotation()); //orientRotation을 켜주면 안넣어줘도 된다.
	// get forward vector
	const FVector Direction = FRotationMatrix(_newDir.Rotation()).GetUnitAxis(EAxis::X);
	if (_HitOut.GetActor()) {
		// 월드에서, AI 폰의 현재 좌표를 기준으로 6미터 이내 16개의 각을 가진 빨간색 원 0.2초간 생성
		DrawDebugSphere(GetWorld(), _HitOut.ImpactPoint, 10.f, 16, FColor::Magenta, false, 1.0f);
		bool isCharacter = (Cast<ACharacter>(_HitOut.GetActor()) == nullptr);
		GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("Debug %f %s %d"),
			_newDir.Length(), *_HitOut.GetActor()->GetName(), isCharacter));
		_targetDistance = (_HitOut.ImpactPoint - _myLocation).Length();
	}

	if (_targetDistance < 200.f) {
		//AddMovementInput(FVector::Zero());
		_pLocation = GetActorLocation() + GetActorRightVector() * 100.f;
	}
	else {
		AddMovementInput(Direction.GetSafeNormal() * DeltaTime * 10.f);
	}
}
 

이득우의 언리얼 C++ 게임 개발의 정석 08.14

교재를 공부하면서 새로 배운 자료 구조, 함수 및 C++ 정보들을 정리하는 글입니다. ● 게임 세계에 속해...

blog.naver.com

 

'강동새싹언리얼수업 > 언리얼' 카테고리의 다른 글

Enemy HP Bar 만들기  (0) 2023.11.28
231128 Widget ItemShop 수업  (1) 2023.11.28
231127 DrawDebugSphere() and HitResult  (0) 2023.11.28
231127 라인트레이스  (0) 2023.11.27
231125 205 특강  (2) 2023.11.25