UKismetMathLibrary::Vector_CosineAngle2D
#include "Kismet/KismetMathLibrary.h"
Access: public
Specifiers: staticUFUNCTIONBlueprintPure
Description
Returns the cosine of the angle between two vectors projected onto the XY plane, ignoring Z. A result of 1.0 means the vectors face the same horizontal direction; -1.0 means opposite directions.
Caveats & Gotchas
- • Z components are ignored entirely. Two vectors pointing in opposite horizontal directions but at different heights will still give -1.0.
- • Returns the cosine, not the angle itself. Pass through FMath::Acos() to get the actual angle in radians.
- • If either vector's 2D (XY) length is near-zero (e.g. pointing straight up/down), the result is undefined. Normalise or check the 2D size before calling.
Signature
static UE_INL_API double Vector_CosineAngle2D(FVector A, FVector B); Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| A | FVector | First direction vector. | — |
| B | FVector | Second direction vector. | — |
Return Type
double Example
Check if an enemy is facing roughly toward the player C++
FVector EnemyForward = Enemy->GetActorForwardVector();
FVector ToPlayer = (Player->GetActorLocation() - Enemy->GetActorLocation()).GetSafeNormal();
double CosAngle = UKismetMathLibrary::Vector_CosineAngle2D(EnemyForward, ToPlayer);
bool bFacingPlayer = (CosAngle > 0.7); // roughly within 45 degrees Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?