# Tram ride

## Recommended Posts

Hi, I would like the tram to stop at the given coordinates for 5 seconds and then continue to the next ones, it works at the first stop, but then it stops after the coordinates

tram = createVehicle(x,y,z)

local stops ={
{x = -2265.1001, y = 701.59998, z = 48.3},
{x = -2264.8, y = 1030.9, z = 82.7},
{x = -1986.2, y = 1287.3, z = 6},
{x = -1668.9, y = 1252.6, z = 6},
{x = -1581.3, y = 981.09998, z = 6},
{x = -1779.6, y = 929.09998, z = 23.7},
{x = -2006.3, y = 882, z = 44.3},
{x = -1601.1, y = 840.59998, z = 6.5},
{x = -1681.8, y = 734.09998, z = 20.4},
{x = -1869.3, y = 608.40002, z = 34.1},
{x = -2006.6, y = 154.2, z = 26.5},
{x = -2234.7, y = -70.1, z = 34.2}
}

local currentStop = 0
local nextStop = 1
local isStopped = false

function moveTramToNextStop()
if not isElement(tram) then return end

local currentX, currentY, currentZ = getElementPosition(tram)
local nextX, nextY, nextZ = stops[nextStop].x, stops[nextStop].y, stops[nextStop].z

local distance = getDistanceBetweenPoints3D(currentX, currentY, currentZ, nextX, nextY, nextZ)
local speed = 0.2

if distance < speed then
if not isStopped then
isStopped = true
setTimer(function()
isStopped = false
currentStop = nextStop
nextStop = (nextStop % #stops) + 1
end, 5000, 1)
end
else
isStopped = false
local ratio = speed / distance
local newX = currentX + (nextX - currentX) * ratio
local newY = currentY + (nextY - currentY) * ratio
local newZ = currentZ + (nextZ - currentZ) * ratio
setElementPosition(tram, newX, newY, newZ)
end
end

setTimer(moveTramToNextStop, 50, 0)

## Create an account

Register a new account