Hey, i recently ran into a problem where i can’t lerp a cframe correctly.
function pointYawTowardsPoint(part, point)
local pointRelative = part.CFrame:PointToObjectSpace(point)
local yawAngleRelative = math.atan2(pointRelative.Z, pointRelative.X)
part.CFrame *= CFrame.Angles(0, -yawAngleRelative+math.rad(-90), 0)
end
This points it against a point at the Y angle, but when trying to lerp it like this:
function pointYawTowardsPoint(part, point)
local pointRelative = part.CFrame:PointToObjectSpace(point)
local yawAngleRelative = math.atan2(pointRelative.Z, pointRelative.X)
part.CFrame = part.CFrame:Lerp(part.CFrame*CFrame.Angles(0, -yawAngleRelative+math.rad(-90), 0),0.1
end
This is the behaviour, it’s supposed to slowly sway to point against the mouse, it works without the lerp but it looks relatively bad.
-- lerp function
local function lerp(a: number, b: number, x: number)
return a + ((b - a) * x)
end
...
local x, prevY, z = part.CFrame:ToEulerAngles()
local positionalCFrame = CFrame.new(part.CFrame.p) * CFrame.Angles(x, lerp(prevY, -yawAngleRelative, 0.1), z)
part.CFrame = positionalCFrame * CFrame.Angles(0, math.pi/2, 0)
i had issues with lerping cframes until i just removed the matrices and things changed pretty quickly
I can tell you i’ve tried every variation of that function, not as offense but i’m really irritated.
CFrame.LookAt is not the answer and i 100% have to use a yaw angle relative.
I’m looking at your code, and the final argument of CFrame:Lerp() seems to always be set to 0.1. There’s your problem: You’re always setting the CFrame to be what it must be at 10% of the lerp’s path.
I don’t know if I’m explaining correctly, but the t argument needs to be set to the percentage of the path that it is at: t should be elapsedTime / totalTime
I told you it is correct, and also yes it is.
I’m not using CFrame.LookAt because i need it to only rotate on the Y axis, no other axis.
Even if you split the angles into X,Y,Z and then only set the Y it doesn’t work, because the other angles just stop it from moving.
:Lerp() will returned a fixed CFrame lerping from the starting point to the destination, t being elapsedTime / totalTime
If you want your lerp to be correct, you need to get the new lerp every frame using the elapsed time and the total amount of time you want your lerp to take.
local plr = game.Players.LocalPlayer
local Base = workspace.Part
local mouse = plr:GetMouse()
local Camera = workspace.CurrentCamera
game:GetService("RunService").RenderStepped:Connect(function()
Camera.CFrame = CFrame.new(Base.Position + Vector3.new(0,10,0),Base.Position)
Base.CFrame = Base.CFrame:Lerp(CFrame.new(Base.Position,Vector3.new(mouse.Hit.Position.X,Base.Position.Y,mouse.Hit.Position.Z)),.1)
end)
Fraction alpha here is the point in time the returned CFrame will be if lerping from point a to point b. You need to update your CFrame by doing the lerp function again with the same CFrames accordingly.