Find Look at Rotation in C++

Hello fellow Programmers,

I came across a problem, when i tried to make a turret look at the player. Other post on the HUB was not helpful to me :frowning:
It was fairly easy in Blueprint.
My problem is that i cant acces the FindLookatRotation() function in C++.

void ATurretC::TickActor(float DeltaTime, enum ELevelTick TickType, FActorTickFunction& ThisTickFunction)
{
	if (PlayerInside)
	{
		FVector PlayerLoc = GetWorld()->GetFirstPlayerController()->GetCharacter()->GetActorLocation();
		FRotator PlayerRot = UKismetMathLibrary::FindLookAtRotation(this->GetActorLocation,PlayerLoc);
		FRotator NewRot = FMath::RInterpTo(StaticMesh2->GetComponentRotation(), PlayerRot, DeltaTime, 2);
	}
	else
	{
		// TODO, Set rot back to Default
		//RootComponent->SetWorldRotation(FRotator(0, 0, 0));
	}
}

Error: ‘FRotator UKismetMathLibrary::FindLookAtRotation(const FVector &,const FVector &)’ : cannot convert argument 1 from ‘overloaded-function’ to ‘const FVector &’
1> Reason: cannot convert from ‘overloaded-function’ to ‘const FVector’

Any other solution to replace UKismetMath Library?

Thank you all in advance,
szuecsg

2 Likes

FRotator newrot = (PlayerLoc - StaticMesh2->GetComponentLocation()).Rotation();

This works fine.

5 Likes

Hmm… I think the error say you are passing the function an overloaded function instead of FVector.
The reason for that is that you are missing parenthesis after GetActorLocation at line 6.
Instead of:

FRotator PlayerRot = UKismetMathLibrary::FindLookAtRotation(this->GetActorLocation,PlayerLoc);

The correct line is:

FRotator PlayerRot = UKismetMathLibrary::FindLookAtRotation(this->GetActorLocation(), PlayerLoc);
5 Likes

Hello !
I think the problem is firstly coming from the fact that you are not actually calling the function GetActorLocation() because you have forgotten the parentheses.

Also, your implementation seem to be not the best way to implement a turret looking at the player (if i got it right). I would suggest to set the actor rotation to the rotation the UKismetMathLibrary::FindLookAtRotation() returns.

It would be in your case something like this :

FVector PlayerLoc = GetWorld()->GetFirstPlayerController()->GetCharacter()->GetActorLocation();
FVector TurretLocation = GetActorLocation();
SetActorRotation(UKismetMathLibrary::FindLookAtRotation(TurretLocation, PlayerLoc));
1 Like