Announcement

Collapse
No announcement yet.

C++ Custom Blueprint Node not working (look rotation)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    C++ Custom Blueprint Node not working (look rotation)

    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
    Code:
    #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
    Code:
    #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 :

    Click image for larger version

Name:	MyLookRotationPB.png
Views:	179
Size:	218.8 KB
ID:	1753025

    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.

    #2
    The problem is that the function you've made isn't static, so it's not being treated as a utility function and you can't access it from anywhere. You can fix that by adding 'Static' to the declaration.

    As for the function itself, what you're trying to do can already be done with this node (there are multiple variations of it).

    Click image for larger version  Name:	rot.JPG Views:	0 Size:	7.7 KB ID:	1753050

    Comment


      #3
      Thank you very much ! It works !
      I had already tried this solution, but after the compilation it didn't work. But by retrying again after your advice, it still didn't work again after the compilation... So I reboot my Unreal Engine and finally it works !

      For the node "Make Rot" i tried but i didn't get exactly what i wanted...
      It's easier for me to use the custom node "My Look Rotation" because i am trying to re-create a code that i did a while ago in unity in c#.

      Thanks again, you allow me to progress in my little project.

      PS : Sorry for my english i did as i could

      Comment

      Working...
      X