Download

Rotate a Vector FRotator to FVector - to turn and look at character

I have an Actor that is a cube, and a thrid view character player that I control around.

I’m trying to convert an FRotator to a FVector so that I can turn the Actor to look towards the Character.

I get error at compilation time, it states that I can’t convert rotation to vector position, but I have seen other examples where people do this in Unreal Engine. I must be missing something, Is it some include ? that I did not add, I looked at the includes to see dependencies but I could not find anything.

Error Message:

**cannot convert argument 1 from 'FVector' to 'FRotator'or the operator cannot be called**

I also get:
 No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I tried different modes.
For example the CCP of my Cube actor sort of NPC part that is suppose to look at the Character.

//Get the location of the character, works fine it gets the location on world of x,y,z of the character.
 FVector CharacterPosition3D = GetWorld()->GetFirstPlayerController()->GetPawn()>GetActorLocation();

//Get Actor Rotation of the cube actor.
FRotator Rotation = GetActorRotation();

//Convert FRotator to FVector, Rotate the Vector Position of the actor towards the character by rotating the vector. 

FVector NewVector = Rotation.RotateVector(CharacterPosition3D);

//Turn The actor towards the Character
SetActorRotation(NewVector);

I get cannot convert Rotation to Vector , operation not supported.

I also tried directly with:

FVector NewVector = GetActorRotation().RotateVector(CharacterPosition3D);

and I eliminated the Rotation variable. Still the same error.

Also did a test to make sure it’s not the components, and added manual values,

FVector SomeVariable = FRotator(30.f, 40.f, 0.f).RotateVector(FVector(20.f,  20.f,  20.f));

Same error, it cannot convert Frotator to FVector, I looked for includes to see if this .RotateVector is tied up to some include somewhere to some library header but I could not find anything.

Looks like that’s how it should work. Are you sure that’s the error that you’re getting, and that it goes to that line?

Error points to line with: SetActorRotation(NewVector);

It’s fine until here, it’s when it tries to put it in practice I guess, the newvector with it’s values it got from the conversion to rotate the vector.

I also tried to add SetActorRotation(NewVector, ETeleportType::None);
I added some includes
#include “Math/Rotator.h”
#include “Math/Quat.h”

But it does not work and I get the same errors.
These are the two errors that pop up in my message log, they point to the same line

**cannot convert argument 1 from 'FVector' to 'FRotator'or the operator cannot be called**

** No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called**

Edit:
I think I understand my mistake. I’m trying to rotate an FVector with a SetActorRotation, that is the problem, FVector NewVector is not an FRotator, and the conversion happens at FVector NewVector, it is distorted there as a vector, so I guess I have to find a way to set it in another way.

How do you set a rotated vector, if I use SetActorLocation it just teleports it to the player.

ah, right, I was concentrating on the Rotating the Vector part.

You want SetActorRotation(NewVector.Rotation());

Yeah but I don’t want to use the built in function Rotation()
I already have a working example with it that is functional.

The conversion function that works is this:
FVector NewVector = (Character - ActorLocationCube).Rotation();

I sort of want to elaborate on my own, Rotation() is not a C++ function, it’s part of the unreal system. Basicly the Rotation() function converts Fvector Location to Rotation, so Rotation points to location. Then I just set the rotation and add (NewVector) without anything.

Problem is that this function does the job of what I’m trying to do manually.

I feel like a gifter.

      FVector ActorLocation  = GetActorLocation();
      FVector Character = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
      FRotator RotateValue = (Character - ActorLocation).Rotation();
      SetActorRotation(RotateValue)

Anyone who wants this, it works. The Cube will be rotated at you with it’s face towards when the game starts.

I want to do this without Rotation();
Or at least understand how Rotation(); works, what is inside of this function.
If you want to learn C++ I guess you will want to do that so you are not in the dark with what you use.

You do have access to the source code

FRotator FVector::ToOrientationRotator() const
{
	FRotator R;

	// Find yaw.
	R.Yaw = FMath::Atan2(Y,X) * (180.f / PI);

	// Find pitch.
	R.Pitch = FMath::Atan2(Z,FMath::Sqrt(X*X+Y*Y)) * (180.f / PI);

	// Find roll.
	R.Roll = 0;

#if ENABLE_NAN_DIAGNOSTIC || (DO_CHECK && !UE_BUILD_SHIPPING)
	if (R.ContainsNaN())
	{
		logOrEnsureNanError(TEXT("FVector::Rotation(): Rotator result %s contains NaN! Input FVector = %s"), *R.ToString(), *this->ToString());
		R = FRotator::ZeroRotator;
	}
#endif

	return R;
}

You sure it’s the Rotation() function ?

It says ToOrientationRotator()

Interesting anyway I will take a look at this function.

yeah they renamed it years ago, left the old one as a call to it

FRotator FVector::Rotation() const
{
	return ToOrientationRotator();
}

https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Core/Private/Math/UnrealMath.cpp

1 Like

Thanks I will be looking at it to see what I can deduce from :sweat_smile: it

1 Like