• News

• Industries

• Learning & Support

• Community

• Marketplace

# Find out if character is looking away to left or right

Hi Guys,

I want to find out if my chracter is looking to the left or the right of a certain point in the world. If my character is looking too far to left or right I have to clamp the character rotation.
Any ideas?
Thanks Pascal

Hi,

dot product of two vectors is defined as cosine of their angle times a product of their magnitudes. For unit length vectors that’s just the cosine. So you take a direction vector from the character to the target point and character’s look direction, normalize them, make a dot product out of it and compare it against the cosine of your maximum deviation angle. If it’s less, then your character is looking too far to the left or right

This would limit angles up and down as well, basically creating a cone where the character can look.

Hope that helps.

Hey,

thanks for the answer. I was probably a little bit unclear about the question. I already got the clamping. Almost the way you described it. But I need to know to which side the player is looking (left or right of the object).
I thought I could do this with the angle , but the angle goes on both sides from 0-180.
I am trying to use the hessesche normal form but no succes so far

Have a look at FMath::GetAzimuthAndElevation. You can extract the azimuth part and do only that if you want.

Or to do it manually:
FVector DeltaToPoint = PointLocation - MyLocation;
FVector DirToPoint2D = DeltaToPoint.SafeNormal2D();
FVector LocalDirToPoint2D = MyRotation.UnrotateVector( DirToPoint2D );
float AngleToPoint = FMath::Sign( LocalDirToPoint2D.Y ) * FMath::Acos( LocalDirToPoint2D.X );

That’s the easy version, if your player character is always oriented Z-up. If that’s not the case, then you need to manually project your direction into the character’s XY plane but the math otherwise remains the same.

-Camille