Check if the player is at the front side of an object

Say I have a door. I want to know if the player is in front of or behind it. To do this I thought I’d get the vector going from the door to the player, compare it to the forward vector of the door, and see if the angle between them falls between 0 and 180 (assuming the range is 0 - 360).

FVector doorToPlayer = player->GetActorLocation() - door->GetComponentLocation();
float angle = FVector::DotProduct(door->GetForwardVector(), doorToPlayer);
angle = FMath::RadiansToDegrees(acosf(angle));

However, the angle is always NAN as soon as I pass it through ascof.

How can I check this properly? I also don’t really know the range returned.

Hello sgp,

You may be better off using a trigger box or a collision shape over the door’s area to get the overlapping actors. As for your issue however, the NaN error would mean that the angle is out of range. This page may be able to help you wish that issue if you wish to pursue that path.


Hope this helps!

The information in the other answer is useful, but my actual problem here is that I just forgot to normalize the doorToPlayer vector.