Please I really need help on this one.
I’m trying to make the floating part tilt on the direction where its going however it’s either too snappy or inaccurate.
Please help and thank you.
local springVelocity = Vector3.new(0, 0, 0)
local springPosition = Vector3.new(0, 0, 0)
local tweenGoalRotation = 0
local tweenStartRotation = 0
local tweenCurrentRotation = 0
local tweenAlpha = 1
local function translate(px, pz, r)
if (r == 0) then
return px, pz
elseif (r == 1) then
return -pz, px
elseif (r == 2) then
return -px, -pz
elseif (r == 3) then
return pz, -px
end
end
local function angleLerp(a, b, d)
local x1, y1 = math.cos(a), math.sin(a)
local x2, y2 = math.cos(b), math.sin(b)
return math.atan2(y1 + (y2 - y1) * d, x1 + (x2 - x1) * d)
end
local function _doLeanCtrl()
-- local prevPos = droot.Position
-- local nextPos = _starterModel.HumanoidRootPart.CFrame * CFrame.new(-2,2,3)
local currentPosition = prevPos
local springPosition = nextPos.Position
local cr = 0
springVelocity = (springVelocity + (currentPosition - springPosition)) * INTERPOLATION_DAMP * 60 * _dt
springPosition += springVelocity
local extentsX, extentsZ = translate(CF.Position.X, CF.Position.Z, cr)
local vx, vz = 9 * springVelocity:Dot(Vector3.new(droot.CFrame.Position.X, 0, 0))/math.abs(extentsX), 9 * springVelocity:Dot(Vector3.new(0, 0, droot.CFrame.Position.Z))/math.abs(extentsZ)
local r
if (not floatEqual(tweenGoalRotation, cr * math.pi/2)) then
tweenStartRotation = tweenCurrentRotation
tweenGoalRotation = cr * math.pi/2
tweenAlpha = 0
end
if (tweenAlpha < 1) then
tweenAlpha = math.min(1, tweenAlpha + _dt/ROTATION_SPEED)
tweenCurrentRotation = angleLerp(tweenStartRotation, tweenGoalRotation, 1 - (1 - tweenAlpha)^2)
r = tweenCurrentRotation
else
r = cr * math.pi/2
end
local effectAngle = CFrame.Angles(math.sqrt(math.abs(vz/100)) * math.sign(vz), 0, math.sqrt(math.abs(vx/100)) * math.sign(vx))
return effectAngle * CFrame.Angles(0, r, 0)
end