In Blueprint there is a node called ‘Random Rotator’ that gives a random rotation. I’m wondering if there is a similar function for C++.
Ofcourse there are several ways to do this myself, but it would be nice to use a single function if it already exists.
I have checked FMath, FQuat, and FRotator but i didn’t find a reference to such a function.
Does anyone know, otherwise, could this be implemented? It would be nice if all blueprint functions have an equivalent C++ function, even nicer if there was some quick way to look up what C++ is similar to what blueprint node (since so many tutorials are only for blueprint)
,
You may wish to look in UnrealMath.h, KismetMathLibrary.h (this is where most Blueprints flow through), UnrealMathUtility.h and UnrealMathVectorCommon.h for implementations that you are looking for.
Hope this helps,
.
Otherwise make it by yourself as a static function:
FRotator UMyHelperCLass::FRandomRotator()
{
const float pitch = FMath::FRandRange(-180.f, 180.f);
const float yaw = FMath::FRandRange(-180.f, 180.f);
const float roll = FMath::FRandRange(-180.f, 180.f);
return FRotator(pitch, yaw, roll);
}
As far as I know most BP functions call an existing c++ function, then just Ctrl+Shift+f in VS and search for keywords you have on the BP node
I already mentioned “Ofcourse there are several ways to do this myself…”
How many hits do you think I would get when I search for the words random and rotator in the complete C++ source code of unreal engine?
this is it:
FRotator UKismetMathLibrary::RandomRotator(bool bRoll)
{
FRotator RRot;
RRot.Yaw = FMath::FRand() * 360.f;
RRot.Pitch = FMath::FRand() * 360.f;
if (bRoll)
{
RRot.Roll = FMath::FRand() * 360.f;
}
else
{
RRot.Roll = 0;
}
return RRot;
}
1 Like