How to tell what side of an object I'm on?

Hi.

I’m trying to figure out how I can tell what side of an object my player is on.

I used to play around with Construct 2 a lot and it had a handy function called something like “is clockwise from”. Where you could basically say "If the player’s angle is clockwise from something else’s angle and their within 15 degrees of that angle then do this, if not it means their counterclockwise so do this instead.

I’m not so terrific with math and angles lol, so I’m struggling to figure out how to do something similar with Blueprints. Is there anyway to tell if my player is within a certain angle from another object?

This one took me back to geometry! I put together a blueprint function that determines whether an actor is clockwise from another. It only determines whether it is clockwise or not, but with a little bit of extra work you could work it into a function that gave the exact angle if you want.

I built this function within the actor I wanted to test (so it returns true if the actor containing the function is clockwise from the provided Other Actor). You could just as easily do this in a Blueprint Function Library and call it with two different actors (just replace the self referenced nodes with the second actor). The whole thing uses the formula:

theta = arccos((X dot Y) / (Length(X) * Length(Y)))

where X is Other Actor’s right vector and Y is the direction vector between Other Actor and this actor.

The result of that comes out of the ACOSd node, giving the angle in degrees between the right vector of the Other Actor and the direction between the two actors. So that means it will be 0 if the tested actor is directly to the right of Other Actor.

Changing Degree Range allows you to narrow the angle that returns true. If you call it with 90 it will return true if the tested actor is anywhere clockwise of the Other Actor.

This can be used to test whether the actor is in front of the Other Actor by simply replacing Other Actor->Get Actor Rotation->Get Right Vector with Other Actor->Get Forward Vector.

Hope that helps!

2 Likes

Absolutely brilliant!

I never would have figured that out. lol Thank you so much :slight_smile:

Legendary, thanks for this

thank you very much