I have a script that moves a car in a specific path and I want the character to be able to ride it if they land on it:
local model = script.Parent
local moveDistance1 = 50
local moveDistance2 = 30
local moveDistance3 = 85
local moveDistance4 = 185
--move and rotate the model
local function moveAndRotate(model, distance, angle)
local primaryPart = model.PrimaryPart
local moveVector = primaryPart.CFrame.LookVector * distance
local rotation = CFrame.Angles(0, math.rad(angle), 0)
local targetCFrame = primaryPart.CFrame * rotation + moveVector
return targetCFrame
end
--tween for smooth transitions
local TweenService = game:GetService("TweenService")
local function tweenTo(part, targetCFrame, duration)
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Linear)
local goal = {CFrame = targetCFrame}
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
tween.Completed:Wait()
end
local rotateDuration = 1
--initial CFrame
local currentCFrame = model.PrimaryPart.CFrame
--first movement and rotation
currentCFrame = moveAndRotate(model, moveDistance1, 10) -- Move forward and turn left 10 degrees
tweenTo(model.PrimaryPart, currentCFrame, 0.5)
--second movement and rotation
currentCFrame = moveAndRotate(model, moveDistance2, 7) -- Move forward and turn left 7 degrees
tweenTo(model.PrimaryPart, currentCFrame, 0.25)
--third movement and rotation
currentCFrame = moveAndRotate(model, (moveDistance3/4)*3, 0) -- Move forward
tweenTo(model.PrimaryPart, currentCFrame, .5)
--third movement and rotation part2
currentCFrame = moveAndRotate(model, moveDistance3/4, 90) -- Move forward and turn left 90 degrees
tweenTo(model.PrimaryPart, currentCFrame, .2)
--final movement and rotation
currentCFrame = moveAndRotate(model, moveDistance4, 0) -- Move forward
tweenTo(model.PrimaryPart, currentCFrame, 2)
I changed my script to use AlignPosition and AlignOrientation and move to certain points, but its very glitchy. I set their rigidity to true and mode to one attachment which is the attachment in the primary part. Also I needed to unanchor the primary part for this which probably doesn’t help.
local car = script.Parent
local alignPosition = car.PrimaryPart:FindFirstChildOfClass('AlignPosition')
local alignOrientation = car.PrimaryPart:FindFirstChildOfClass('AlignOrientation')
local movePoints = {
workspace.Point1.Position,
workspace.Point2.Position,
workspace.Point3.Position,
workspace.Point4.Position
}
local rotations = {
Vector3.new(0, 10, 0),
Vector3.new(0, 7, 0),
Vector3.new(0, 90, 0),
Vector3.new(0, 0, 0)
}
local function moveToPoint(pointIndex)
if pointIndex > #movePoints then
car:Destroy()
return
end
local targetPosition = movePoints[pointIndex]
local targetRotation = rotations[pointIndex]
alignPosition.Position = targetPosition
local reachedTarget = false
local connection
connection = game:GetService("RunService").Stepped:Connect(function()
if (car.PrimaryPart.Position - targetPosition).magnitude < 1 then
if not reachedTarget then
reachedTarget = true
connection:Disconnect()
alignOrientation.CFrame = CFrame.Angles(math.rad(targetRotation.X), math.rad(targetRotation.Y), math.rad(targetRotation.Z))
moveToPoint(pointIndex + 1)
end
end
end)
end
moveToPoint(1)
this method worked the best. I made my car constantly move forward then turn after a certain distance. assemblyAngularVelocity was making my car slide around so I just used CFrame. but my cars seem to not always take the same path when they spawn in, causing it to sometimes crash into objects on the side, even though my road is flat.
local carModel = script.Parent.PrimaryPart
local speed = 60
local distanceMoved = 0
local function moveCarForward()
local forwardVector = carModel.CFrame.lookVector
carModel.AssemblyLinearVelocity = forwardVector * speed
end
local function rotateCar(degrees)
carModel.CFrame = carModel.CFrame * CFrame.Angles(0, math.rad(degrees), 0)
end
local rotation1 = 10
local rotation2 = 7
local rotation3 = 90
local rotation4 = 25
local function moveCarCoroutine()
while true do
moveCarForward()
task.wait()
end
end
local function rotateCarCoroutine()
while true do
distanceMoved = distanceMoved + speed
if distanceMoved >= 2000 then
rotateCar(rotation1)
rotation1 = 0
end
if distanceMoved >= 3000 then
rotateCar(rotation2)
rotation2 = 0
end
if distanceMoved >= 8500 then
rotateCar(rotation3)
rotation3 = 0
end
if distanceMoved >= 16000 then
rotateCar(rotation4)
rotation3 = 0
break
end
task.wait()
end
end
task.spawn(moveCarCoroutine)
task.spawn(rotateCarCoroutine)
Wdym glitchy? You set MaxVelocity, right? You really needed to unachor the primary part in order to make this work, but you can use WeldConstraint if you want a part to stick to it (also in order to work, every part connected to the car must be unachored and connected to the primary part via WeldConstraint)
If I’m correct, friction only works when there is a velocity, if you directly modify the position of a part without it’s velocity you will no longer be able to move with it.
yeah everythings been unanchored and welded. i’m just saying it seemed to work better in my original script when the primary part was anchored, now that it’s unanchored with the rest, i feel like it’s contributing to the glitchy movement. sometimes it vibrates when it moves and it’s really bad when it makes the sharp 90 degree turn
yes plus i am testing this by running the server not playing it on a client. it jitters a bit when reaching each point and flings when it does the 90 degree rotation. also the 90 degree rotation is really slow.
here is testing it on a part:
I think I got it to work now, previously I had AlignPosition with rigidity enabled so I couldn’t set a max velocity. I had it enabled before because my car seemed to be unable to move without it. But I did some tinkering and disabled it, set the MaxForce to 1000000, MaxVelocity to 100, and Responsiveness to 200 and now it moves pretty well. Thanks