FMath::Atan2
#include "GenericPlatform/GenericPlatformMath.h"
Access: public
Specifiers: static
Description
Returns the angle in radians between the positive X axis and the point (X, Y), in the range [-π, π]. Handles all quadrants correctly including when X is zero.
Signature
static CORE_API float Atan2(float Y, float X) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Y | float | The Y component (numerator). | — |
| X | float | The X component (denominator). | — |
Return Type
float Caveats & Gotchas
- • The parameter order is (Y, X), not (X, Y) — this matches std::atan2 but is easy to swap accidentally.
- • Returns radians. Use FMath::RadiansToDegrees to convert to degrees for use with FRotator.
- • When both X and Y are zero, the result is implementation-defined (typically 0). Guard this case if the input can be a zero vector.
Example
Compute facing angle from a 2D direction vector C++
// Get the yaw angle to face a target in the XY plane
FVector ToTarget = (TargetLocation - GetActorLocation()).GetSafeNormal();
float YawRadians = FMath::Atan2(ToTarget.Y, ToTarget.X);
float YawDegrees = FMath::RadiansToDegrees(YawRadians);
SetActorRotation(FRotator(0.0f, YawDegrees, 0.0f)); Tags
Version History
Introduced in: unknown
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?