How can I see if a player is looking at a created actor?

Hi, is there a method to see if a Player is looking at an actor I’ve created? I’m making a race game, and I wanna see if the Player is on the wrong way or not.

Hi ,

try running a trace from the player to the actor. if the trace does not return true your player is not facing it and you can act accordingly.

I did this, but it never says: “Wrong way!!”

http://puu.sh/9YeNM/1774b8442b.jpg

http://puu.sh/9YeWJ/3141737c47.png

I have added a possible solution in your forum post:

The project’s 3D artist has replied.

You can use some trigonometric math to test the angle of the car in relation to the next waypoint on the track.
The ACOSd at the end of this blueprint logic outputs the angle of “Self” in relation to another Actor. If it’s greater than 90* or less than -90*, you know the actor is facing the wrong way. I would set this up at an timing interval instead of using Tick, but it’s in this to output the Current Relative Angle.

Angular Formula: VectorA Dot VectorB = VectorA length × VectorB length × cos(θ)

(see Dot Product for detailed visuals of what we the above is attempting to accomplish).

1 Like

Thanks for the help, it works! You are the best.

thank you x2

It doesn’t work for me help!

oh also it just prints out 90 every time

and i have it in the blueprint that i want to be testing for .

A simpler way would be to take the actor’s location minus your character’s location, normalize it and take this normalized vector’s dot with your character’s forward vector. If the dot is greater than 0.9 then your actor is facing the correct way.

The dot’s value can go from (-1) to 1 :

  • 1 means your character is looking perfectly towards the actor
  • 0 means your character is looking in a direction perpendicular to the direction you want him to face
  • (-1) means your character is looking perfectly in the opposite direction

As such, 0.9 is a value that allows for a bit of tolerance but will still allow for pretty accurate results.

1 Like

Still super helpful in 2021 for those of us who are mathematically illiterate! Thanks so much!