How to get an angle between 2 Vectors?

Is there a built in function to find the angle between 2 points (2D and 3D) for HUD and World?

I love SOH CAH TOA, but l love clean code/blueprints more

6 Likes

Whats Up Demonicated,

Have you tried the Node Find Look At Rotation? This returns angle of rotation between two points. By plugging in just the X & Y into a vector for 2D, you obtain similar results but one less axis.

Peace

1 Like

The best way is to actually make the function you need. It’ll work for any vector (2d or 3d).

You need to INPUT TWO DIRECTION VECTORS in WORLD SPACE.

First. Make a new function.
Make it have 2 inputs - VectorA and VectorB - and one output - a float.

Take the two vector values and normalize them.

Then take the two results and find the Dot product.

Then use the ACos Node to find the inverse Cos of the Dot product.

Voila.

14 Likes

Nice. So the float essentially = degrees or do you need another step to finish the conversion?

The ACos function has two possible outputs - degrees and Radians. Up to you which you prefer.

Exactly what it should be.
Here is the equivalent code.
float AimAtAngle = ((acosf(FVector::DotProduct(PlayerDirection, MouseDirection))) * (180 / 3.1415926));

EdwardCoughlan, note that you could replace:

float AimAtAngle = ((acosf(FVector::DotProduct(PlayerDirection, MouseDirection))) * (180 / 3.1415926));

with this to make it a little cleaner:

float AimAtAngle = FMath::RadiansToDegrees(acosf(FVector::DotProduct(PlayerDirection, MouseDirection)));
1 Like

Hey guys !

Just a quick question…I recreated the above graph in the image and the angle is ok but it’s always positive. I need this for an ai to determine if it should go left or right. Could you please show me how I could get the sign of the angle as well ? I know there is a way using cross product but I haven’t figured it out yet. Thanks !

just insert a simple bool, if Xend-Xini <0, the angle is going to the left, else its to the right. adjust to your needs. also, this is a 2 years old one, you should open a new one.

1 Like

Hey jblaswu ! Thanks for the quick response. Sorry about the post thing, I thought it would be more appropriate to post it here. Anyway…who is Xend and Xini ? this is for an AI controller…not the player. The AI doesn’t know where left and right is…I am giving it an angle and it just moves in that direction. The problem is that the angle is always positive.

Hey Cosmos,
It depends on exactly what sort of AI you’re making and what sort of functionality you’re trying to achieve.
I’d use the find look at rotation for the most part.
More info?

It’s a dual character system similar to last of us. Player controls one character and the AI controls the other character. What I need is to give the AI an angle so that I can trigger the animation for it to turn towards the player. (I am using root motion for both characters). I just realised that even if I get the right angle it won’t do the job because AI needs the navigation path otherwise it would turn into a wall.

Oh, dude.
Just use the inbuilt navigation system if you can. Go check out those tutorials. It creates the path then follows it around obstacles on the navmesh, you just give it a target position.

Simple and best - thx!

This is from the VR Expansion Plugin for the “shortest” angle between two vectors.

FVector vec1;
FVector vec2;

float Angle = 0.0f;
FVector nAxis;

FQuat BetweenQuat = FQuat::FindBetweenVectors(vec1, vec2);

BetweenQuat.ToAxisAndAngle(nAxis, Angle);

Angle will then be in radians.

3 Likes

For those like me who rely on this angle in order to get a result between -180 and 180; for exemple to make an animation go Clockwise or CounterClockwise, you need to subtract the atan of the second vector to the first like so:

3 Likes

Note that Unreal Math already has a function to “unwind” any angle to -180-180: FMath::UnwindDegrees. I’m not sure if it’s directly supported in Blueprints, but you could wrap it if not.

In Blueprints - “Clamp Axis” function

Clamp Axis returns an angle between 0 to 360 degrees.

Yes, but also changes the -180/180 degree to positive values. I should write more clearly