Search code examples
luagame-developmentroblox

Make a model go to a desired place by going one by one in waypoints


So I'm trying to make a game with a car cutscene where the car drives up to a house in waypoints, similar to pathfinding

[1

So it goes to point 1, stops, and then goes to point 2 and ends, but not in a straight line, so it goes to the parts that are in a folder like waypoints that are placed or something


Solution

  • Use more waypoints, so each path from waypoint to waypoint is a straight line, and traverse it using a for loop or other iteration methods, you can choose between lerping, tweening or anything. Though lerping might be the smoothest without cuts inbetween waypoints.

    Code could be

    local car = ...
    local waypoints = {...}
    
    local _maxValue = 100 -- over how much iterations it should take to reach the next waypoint
    
    for _, waypoint in ipairs(waypoints) do
        for i=1, _maxValue do
            car:PivotTo(car:GetPivot():Lerp(CFrame.new(waypoint.Position), i/_maxValue))
        end
    end
    

    In reality, you might want to tweak the code to ensure the car won't rotate in weird directions all of a sudden.