• News

• Industries

• Learning & Support

• Community

• Marketplace

Determine which actor's direction i'm facing

Hi all!
Just one simple question!

Taking a cube, how can I determinate which face (Forward, Backward, Left, Right) the player is facing?

This below is the function i’m currently using but is not 100% accurate and sometimes fails its job and i was wondering if there are other methods

Thanks!

An image explain better what i’m triying to achieve so here it is

you need convert player coords into new basis (probably you must miltiply by matrix, i forget)
then, (x,y)=side1; (x,-y)=side2 … etc

look math=)

Does this fail its job when the player is close to 45°, 135°, 225° and 315° to the forward vector of the cube? This would make sense to me. What do you actually mean when you say it fails? Do 2 of the equal nodes become true? Or what’s happening?

Generally, trigonometric functions in computers are pretty tricky. They are never exact. Though I don’t think the accuracy of the ACosd node is the problem. What you’re doing looks correct for me but overly complicated. Why don’t you just calculate the angle to the actor forward vector and check if it’s between 45° and 135°, 135° and 225° and so on. That is way less error-prone I think. Is there generally a reason you’re doing this in 3D space? Doing everything in 2D will have less sources of error. You can get the angle between vectors in the range 0,360) by using the determinant in addition to the dot product.

You’re currently calculating a trigonometric functions 7 times. My approach would only need 1. Trigonometric functions do need a lot of operations. What you do may become pretty heavy on performance when you need to execute your logic often times.

Thanks for the quick replies guys, I appreciated that.

This is the solution I found and It seems to work pretty well and it is more accurate than the previous version.

I just used the “Find Look at Rotation” function minus the actor yaw rotation and then clamp the axis to get the 0 ° - 360 ° range

guys sorry if i’m still bothering but i need help in how to figure this:
using the method i shown how can i take the actor’s scale in consideration?

For instance in this situation where the scale is 2:1

scaling actors in this manner bad idea)

Replace the borders the following:

45° = ArcTan(1,1) -> ArcTan(ScaleX,ScaleY)
135° = ArcTan(-1,1) -> ArcTan(-ScaleX,ScaleY)
225° = ArcTan(-1,-1) -> ArcTan(-ScaleX,-ScaleY)
315° = ArcTan(1,-1) -> ArcTan(ScaleX,-ScaleY)

Hey Pepeee
thanks for the support, I understood the math you suggested but, i didn’t fully understand how to replace the degrees range with blueprints (specifically how to use the “Atan” function).
Could you be so kind to show me how to replace the degrees within the bp graph?

It would look like this:

Though, you should definitely precompute the 4 values and store them in variables instead of calculating them each time you run this.