1. What do you want to achieve?

Hello. I want to calculate the distance needed for when my train should start decelerating.

  1. What is the issue?
    I’ve tried looking online or using AI, but the solutions do not work properly and either overshoot or underrun and its usually unpredictable. Most solutions only work in 2 dimensions, not 3D.

My current code is a mix of my work and AI, which does not work. The value given is much more than the distance every time.

function getStopDistance(stop:Vector3)
	local currentSpeed = trig.AssemblyLinearVelocity.Magnitude
	RunService.Heartbeat:Wait()
	RunService.Heartbeat:Wait()
	
	print(currentSpeed)
	local distance = (trig.Position-stop).Magnitude
	print("dist: "..tostring(distance))
	
	local decel
	if direction.Value == -1 then
		decel = ((0.066)* 0.13)
	end
	if direction.Value == 1 then
		decel = ((0.66)* 0.11)
	end
	
	local distanceToStop = ((currentSpeed^2) / (2 * decel))

	return distanceToStop --math.abs(distance-distanceToStop)
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.