Hello, I would like to make a tank move smoothly across the ground, like the tank from TF2, this video should help you understand what I mean.
See how it makes smooth corners and dynamically changes its angle? That is what I would like to pull off. But how exactly can I do this?
what are you trying to make exactly?
to make a tank move like the one shown in the video, on a set path
i know about but for what thing?
i don’t understand what you are asking
what are you working on?
are you trying do a tower defense thing or smth?
no, im not sure what kind of game you’d categorise mine in, but i simply need to be able to move a model on a set path with smooth corners and dynamic angle changing like in the video.
alr so
Bezier modulescript
local bez = {}
function Lerp(p0, p1, t)
return (1 - t) * p0 + t * p1
end
function parse(index,bp,T)
bp[index] = Lerp(bp[index],bp[index+1],T)
end
function bez.GetBezierPoint(T, ...)
local bp = {...}
parse(1,bp,T)
parse(2,bp,T)
parse(1,bp,T)
return bp[1]
end
function getCframe(Point, NextPoint)
return CFrame.lookAt(Point, NextPoint, Vector3.new(0, 1, 0))
end
function bez.getCF(i,c,posPoints)
local inc = (1 / c)
local Point = bez.GetBezierPoint(i, unpack(posPoints))
local NextPoint = bez.GetBezierPoint(i + inc, unpack(posPoints))
return Point, getCframe(Point, NextPoint)
end
return bez
main script
local bez = require() --insert module
function convertLapsePoints(child)
local prefix = "p"
local limit = 0
local getlapses = {}
for i = 1,math.clamp(#child:GetChildren()-1,0,math.huge) do
local name, nextname = prefix..i, prefix..(i+1)
local pointA, pointB = child:FindFirstChild(name), child:FindFirstChild(nextname)
local distance = (pointA.Position-pointB.Position).Magnitude
local info = {
["lapse"] = tonumber(distance/math.pi),
["points"] = {
pointA,
pointB
},
["curvatureObject"] = pointA:FindFirstChild("curvature"),
["last"] = limit,
}
if info.curvatureObject then
local distanceA = (pointA.Position-info.curvatureObject.Position).Magnitude
local distanceB = (info.curvatureObject.Position-pointB.Position).Magnitude
local editeddistance = distanceA+distanceB
info.lapse = tonumber(editeddistance/math.pi)
end
limit += info.lapse
getlapses[i] = info
end
return getlapses, limit
end
function set(getlapses, limit, ModelToMove, Velocity)
local progress = Instance.new("NumberValue",ModelToMove)
local currentindex = 1
local modifier = 1
local function multi(vector3,by)
return Vector3.new(vector3.X*by,vector3.Y*by,vector3.Z*by)
end
local function curvature(points,p,l,c)
local alpha = (p/l)
local fix = c or 40
local p1,p2,p3 = table.unpack(points)
local newVector, CF = bez.getCF(alpha,fix,{p1.Position,p3.Position,p2.Position})
return newVector, CF.Rotation*CFrame.Angles(0,0,math.rad(90))
end
local function transcurse(pA,pB,cur,l,b)
if cur then
local distanceA = (pA.Position-cur.Position).Magnitude
local distanceB = (cur.Position-pB.Position).Magnitude
modifier = ((distanceA/distanceB)*((l-b)/l))+((distanceB/distanceA)*(b/l))
return curvature({pA,pB,cur},b,l)
else
modifier = 1
return ((multi(pA.Position,((l-b)/l)))+(multi(pB.Position,(b/l)))), CFrame.lookAt(pA.Position,pB.Position).Rotation*CFrame.Angles(0,0,math.rad(90))
end
end
local function update(obj:Model)
for i = 1,#getlapses do
local info = getlapses[i]
if (progress.Value < info.lapse+info.last+boolnum[i==#getlapses]) then
local pointA, pointB = table.unpack(info.points)
local position, rot = transcurse(pointA,pointB,info.curvatureObject,info.lapse,progress.Value-info.last)
obj.PrimaryPart.CFrame = CFrame.new(position)*rot
currentindex = i
break
end
end
end
local connection
connection = runservice.Stepped:Connect(function(time, delta)
progress.Value += (Velocity/modifier)*delta
update(ModelToMove)
if (progress.Value >= limit) then
if connection~=nil then
connection:Disconnect()
end
end
end)
end
Well, thank you. I appreciate it.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.