• News

• Industries

• Learning & Support

• Community

• Marketplace

# Get angle offset from forward vector (Need Help)

Hello Everyone!

I will try to explain this as best as I can…

I am trying to get an actor’s angle compared to my player’s position. Meaning that an actor in front of the player would have an gle of 0, and an actor at the right of the player would have an angle of 90.

Similar to the “Calculate Direction” node, I am looking to get a value Between 0 and 180 if the actor is on the right of the player, or between -0 and -180 if the actor is on the Left of the player.

Everything I tried gave me some weird results as the actor got more behind the player rather than in front.

Can anyone please point me in the right direction?

Get forward vector of player, get vector from player to actor.
Normalize both vectors (you must normalize them or else results will be seemingly random).
Calculate dot product of both normalized vectors.
Result is COSINE of angle between those vectors.
Calculate arcus cosinus you get angle.

PS. if your game does not happen much in Z axis (ie mostly flat), before calculating normal vectors, multiply both by vector [1,1,0] you will get “flat” angle this way.

1 Like

This partially work, it give me a value of 0 when in front and 180 when behind, but the value is always positive… So I cannot determine wether the actor is to the left or right of the player. But the angle is good haha.

Substracting the actor’s Location from the player’s location give us X and Y values wich reprensent the sides of the triangle we are grabing the angle from using ACOSD, Right?

You can get dot product of right vector, get its SIGN. Or check value that it has at +90 and -90 and do branch.

I was quite sure that dot product should give results from -1 to +1.
Print out value of dot product, it may be arcus cos that kills negative values.

you need to substract those locations to get direction vector from pawn to actor.
locations and vectors mixed together really confuse even some smart people.

Or you make cross product of those same 2 vectors. If it points up actor is to the right, if points down actor is to the left.
So do cross product of forward and direction to actor, and take sign of Z value from result.

Just remember to comment it all. You may forget that dot and cross products in time.

Also for anybody in future that reads this:

Honestly, It really is just because of the trigonometry, well angles in general give me a hard time x) Anything else is fine.

And I am having a hard time understand what you mean by just following some text as well. Is what I did ok? or did I do it wrong?

Yes your graph is exactly what i described.

For getting left\right direction:

• add CROSS product of forward and that direction to actor vector.
• check its result Z value, if its positive your angle is to the right, if Z is negative angle is to the left
• or simply get SIGN out of Z value from CROSS product result and multiply that sgin result by degrees from arc cosine.

No worries, i have friend which is doing game with me. And he is scared of whole vector math. Or rather quite confused by it. Its common to scratch head when that is involved. Luckily or not i was forced to learn magnetic field theory which is full of vectors, that is why i remember dot and cross products.

Thank you so much! The nightmare is now over hahaha it works

By the way, this was an improvement of my enemy targeting system, Before that I used distance, This is much better.
So now when you press the button, the camera lock onto the closest enemy and you can switch left and right instead of switching from closer to farther. It just feel more natural.

Here is the completed Graph:

Great it works.

OH btw, some tips:

• you do not need to use “Break vector” and “make vector” nodes, instead right click on yellow vector pin and select “split struct”. node pin must be unconected for it to work.
• or instead of breaking and setting Z to zero you multiply vector by [1,1,0]

Thanks for these tips! “split struct” is amazing, and I prefer the combination of split and make vector VS multiply as the multiply node is super long and flat, just for the look.
my Macro is now more compact (I’m very OCD about my blueprints beign square and compact and very well ordered)

Also note that this chunck of code is inside a macro in wich there is nothing else, and I place this macro where I need it in my graph. So I really do not know what to comment haha, this whole thing is in a macro called “get rotation from player”.

Now this is one little happy Macro:

Put it into function inside function library.
Make 2 inputs: other actor, and pawn

Then make it PURE function. tiny checkbox in properties on the right.

Yes, that OCD thing, I code together with friend, such dual coding speeds up coding a lot, we alternate and while one is coding second is watching for all errors. But due to us both having OCD with graphs sometimes we waste hours on moving stuff around.

What is it gonna change if I do that?

You will get tiny custom node that calculates angle between 2 actors. Later this can be used for turret logic, or rotating AI bots, or checking if they should see your pawn or not.

Also thinking of it add 2 actor references as input. It will be more flexible. kind of “make look at rotation” node.

Try pure functions, they are great.

Ohhhhh ok So functions made inside of Functions library can be accessed from any blueprint, and to make it pure mean it will have no execution pin, Just like the macro I had done.

Meaning I can now use these macro in any blueprint, Man I am glad I made this thread, you just teached me so many usefull things ^^

Well … For me the Z value of vector is always 0 (the one before Sign) but when I do not split structure and display it → It has -0.000 or 0.000 … what the heck is going on?

Excellent! This worked perfectly for doing a cone like detection for me.

I tried to use the last chart. But it didn’t work for me. I have a player and a horse. I create a sphere trace to detect the horse. I’m trying to find out if the player is standing on front/left/right/rear of the horse so that I can play certain mounting animation.

Old post but seen a couple of people afterwards having trouble…

For anyone else getting this issue, you might be confusing “Cross Product” with “Vector * Vector”, make sure you search for “Cross Product” just before the "Sign (float)"and it should work for you.

1 Like