UKismetMathLibrary::ToDirectionAndLength2D
#include "Kismet/KismetMathLibrary.h"
Access: public
Specifiers: staticBlueprintPure
Description
Decomposes a 2D vector into its unit direction and scalar length in a single call. More efficient than calling NormalSafe2D and VSize2D separately because the length is computed only once.
Caveats & Gotchas
- • If the input vector is zero-length, OutDir is set to (0,0) and OutLength to 0 — callers should guard against using OutDir as a movement direction in this case.
- • OutLength is a double, while many UE movement parameters are float. An implicit truncation will occur if you pass OutLength directly into float parameters without casting.
Signature
static UE_INL_API void ToDirectionAndLength2D(FVector2D A, FVector2D &OutDir, double &OutLength); Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| A | FVector2D | Input 2D vector. | — |
| OutDir | FVector2D& | Receives the unit-length direction, or (0,0) if A is near-zero. | — |
| OutLength | double& | Receives the original length of A. | — |
Return Type
void Example
Decompose velocity into speed and direction for movement logic C++
FVector2D Velocity = GetVelocity2D();
FVector2D Dir;
double Speed;
UKismetMathLibrary::ToDirectionAndLength2D(Velocity, Dir, Speed);
if (Speed > 10.0)
ApplySteering(Dir, static_cast<float>(Speed)); Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?