Garry's Mod Wiki

Vector:DistToSqr

number Vector:DistToSqr( Vector otherVec )

Description

Returns the squared distance of 2 vectors, this is faster than Vector:Distance as calculating the square root is an expensive process.

Squared distances should not be summed. If you need to sum distances, use Vector:Distance.

When performing a distance check, ensure the distance being checked against is squared. See example code below.

Arguments

1 Vector otherVec
The vector to calculate the distance to.

Returns

1 number
Squared distance to the vector.

Example

Checks if a player is within dist units of another player in the most efficient way possible.

function PlayerWithinBounds( ply, target, dist ) -- Square the input distance in order to perform our distance check on Source units. local distSqr = dist * dist return ply:GetPos():DistToSqr( target:GetPos() ) < distSqr end print( PlayerWithinBounds( Entity(1), Entity(2), 500 ) )
Output:
true

Page Links


Special Pages


Wikis

?

Render Time: 82ms

DB GetPage 46
Generate Html 5
SaveChanges (1) 10
Render Body 0
Render Sidebar 13