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.
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.
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.
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”.
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.
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.