Easy way to get vector length?

Right now I’m doing this:

float vectorLength = FMath::Sqrt(FMath::Pow(someVector.X, 2) + FMath::Pow(someVector.Y, 2) + FMath::Pow(someVector.Z, 2));

I did find FVector::ToDirectionAndLength, but I must have been using it wrong cause the game would crash. All I needed was the length anyway.

Did you think to check out the header file for FVector?

FORCEINLINE float FVector::Size() const
{
	return FMath::Sqrt( X*X + Y*Y + Z*Z );
}

float vectorLength = someVector.Size();

Ah, I was looking for length, not size. Thanks.

This must be a really common misconception. Even the documentation clarifies that size is actually length/magnitude and also says that the Size() returns the length of the vector (not the “size”). :c

Just InCase if anyone is searching for getting the magnitude of the character velocity

if (GetCharacterMovement()->Velocity.Size() == 0)
{
//Character not moving or some bool to store the info
}

To check if your character is moving or not !!

1 Like