Suggestions for working with Magnitude?

Hello, I’m currently experimenting different methods of magnitudes for a Spaceship system I am working on.
The magnitude is to confirm whether or not the Player is within a suitable range of a Planet / Location in order to access it.

Methods I have tried:
Constantly ‘while true do’ - To constantly get the Ship’s root position and dictate whether or not it is within a distant.

Creating a false CFrame value of the Ship’s root position that is updated every (1) seconds, then when is .Changed() will fire the function to dictate whether or not the ship is within a suitable distance.

I’m open to any other methods that may be more performance friendly than mine.
Thank you!

local lastupdate = tick()
game:GetService("RunService").Heartbeat:Connect(function()
   local t = tick()
   if t-lastupdate>=1 then
      lastupdate = t
      --check
   end
end)
1 Like

Instead of using magnitude which is not performant due to the square root you could try converting the position of the player to a local position to the planet, i like to call this point collision

while true do
local RelativePosition = Planet.CFrame:Inverse() * Root.Position
local Size = Planet.Size/2
local X,Y,Z = math.abs(RelativePosition.X),math.abs(RelativePosition.Y),math.abs(RelativePosition.Z)
if X < Size.X and Y < Size.Y and Z < Size.Z then
print(“Player detected!”)
end

wait(1)

end

1 Like