#how to count a distance in studs

42 messages · Page 1 of 1 (latest)

placid crescent
#

like, in 2d game i need to count the distance with script in studs. here is example:

#

there can be more bounces, but i drew only 1

fervent depot
#

Magnitude?

urban lantern
fervent depot
urban lantern
#

maybe something like

fervent depot
#

just use magnitude

#

(Position 1 - Position 2).Magnitude

urban lantern
#
local distance = 0
local last_pos = ball.Position

RunService.Heartbeat:Connect(function()
  distance += (last_pos - ball.Position).Magnitude
  last_pos = ball.Position
end)
fervent depot
#

that will just keep adding the distance to the distance

#

distance will be a huge number

urban lantern
#

a

fervent depot
#

like if its 1 stud away then its going to keep adding 1 to distance every heartbeat

urban lantern
#

wdym

fervent depot
#

RunService.Heartbeat:Connect(function()
distance += (last_pos - ball.Position).Magnitude

urban lantern
#

if its still then last_pos = ball.Position

#

it checks how much its moved every heartbeat

#

diagram

fervent depot
#

you are using += it just keeps adding the distance to the variable "Distance"

#

idrk

urban lantern
#

if the ball is at 0,0,0

#

last_pos is at 0,0,0

#

0 is added

#

next heartbeat it moves to 1,0,0

#

last_pos is at 0,0,0

#

1 is added

#

next heartbeat its still at 1,0,0

#

last_pos is at 1,0,0

#

0 is added

fervent depot
#

hmmm

#

makes sense ig

#

oh wait you werent updating the last_pos earlier so I thought u were trying to-

#

nvm

#

just checked the edited

urban lantern
#

modified discord client

fervent depot
#

and I might or might not be saying that so discord might or might not ban me

urban lantern
#
local distance = 0
local last_pos = ball.Position

RunService.Heartbeat:Connect(function()
  distance += (last_pos - ball.Position).Magnitude
  last_pos = ball.Position
end)
#

something like this