Hello, beginner for about 3 weeks with UE4, I have so far only used blueprints. But having needs of a similar lookrotation function like in Unity.
While searching I found this post where the author offers a c++ code for this needed function :
https://forums.unrealengine.com/deve…se-who-need-it
Having very slight c++ knowledge, I used the provided code and created a MyLookRotation.h file and another MyLookRotation.cpp that here :
MyLookRotation.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyLookRotation.generated.h"
/**
*
*/
UCLASS()
class CAMERATEST2020_API UMyLookRotation : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure, Category = "My Nodes",
meta = (DisplayName = "My Look Rotation", Keywords="my look rotation"))
FRotator MyLookRotation(FVector lookAt, FVector upDirection);
};
MyLookRotation.cpp
#include "MyLookRotation.h"
FRotator UMyLookRotation::MyLookRotation(FVector lookAt, FVector upDirection)
{
FVector forward = lookAt;
FVector up = upDirection;
forward = forward.GetSafeNormal();
up = up - (forward * FVector::DotProduct(up, forward));
up = up.GetSafeNormal();
///////////////////////
FVector vector = forward.GetSafeNormal();
FVector vector2 = FVector::CrossProduct(up, vector);
FVector vector3 = FVector::CrossProduct(vector, vector2);
float m00 = vector2.X;
float m01 = vector2.Y;
float m02 = vector2.Z;
float m10 = vector3.X;
float m11 = vector3.Y;
float m12 = vector3.Z;
float m20 = vector.X;
float m21 = vector.Y;
float m22 = vector.Z;
float num8 = (m00 + m11) + m22;
FQuat quaternion = FQuat();
if (num8 > 0.0f)
{
float num = (float)FMath::Sqrt(num8 + 1.0f);
quaternion.W = num * 0.5f;
num = 0.5f / num;
quaternion.X = (m12 - m21) * num;
quaternion.Y = (m20 - m02) * num;
quaternion.Z = (m01 - m10) * num;
return FRotator(quaternion);
}
if ((m00 >= m11) && (m00 >= m22))
{
float num7 = (float)FMath::Sqrt(((1.0f + m00) - m11) - m22);
float num4 = 0.5f / num7;
quaternion.X = 0.5f * num7;
quaternion.Y = (m01 + m10) * num4;
quaternion.Z = (m02 + m20) * num4;
quaternion.W = (m12 - m21) * num4;
return FRotator(quaternion);
}
if (m11 > m22)
{
float num6 = (float)FMath::Sqrt(((1.0f + m11) - m00) - m22);
float num3 = 0.5f / num6;
quaternion.X = (m10 + m01) * num3;
quaternion.Y = 0.5f * num6;
quaternion.Z = (m21 + m12) * num3;
quaternion.W = (m20 - m02) * num3;
return FRotator(quaternion);
}
float num5 = (float)FMath::Sqrt(((1.0f + m22) - m00) - m11);
float num2 = 0.5f / num5;
quaternion.X = (m20 + m02) * num2;
quaternion.Y = (m21 + m12) * num2;
quaternion.Z = 0.5f * num5;
quaternion.W = (m01 - m10) * num2;
return FRotator(quaternion);
}
With this, I have a “My Look Rotation” node, however the pin target send me an incompatibility error when I want to link a component to it, and the actions for blueprint panel doesn’t contain any associable object :
I searched for long hours for solutions, but my abilities in English and for c++ probably played on the fact that I can’t find the solution, that’s why i ask your help please.