Need help with lerping CFrame

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.

2 Likes

Sorry for the bump, but i need help with this.

1 Like

Have you tried to lerp the angles separately?

Example:

-- 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

2 Likes

u can probably just do this

Mouse.CFrame = Mouse.CFrame:Lerp(CFrame.new(Mouse.Position,mouseHitPos),0.1)
1 Like

This usage of CFrame.new() is deprecated. Use CFrame.lookAt() instead.

1 Like

Can’t use either, must use this function with specifically the Y angle. I do not want rotation in at any other axis.

1 Like

oh then u can just do this

Mouse.CFrame = Mouse.CFrame:Lerp(CFrame.LookAt(Mouse.Position,Vector3.new(mousehitPos.X,Mouse.Position.Y,mousehitPos.Z)),0.1)

This will change the look vector of the mouse without changing the Y position

1 Like

this seems to do the same exact thing,
here’s what happens.

1 Like

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.

1 Like

How exactly is the mouse build?
Is it a part
what part type
what decal it uses

1 Like

It’s a mesh part that’s set up correctly.

1 Like

can u add decal and tell me if the Orange outline is the same direction as the mouse face

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.

how would i go about this?

3
0
CHAR

: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 originalCFrame = part.CFrame
part.CFrame = originalCFrame:Lerp(originalCFrame * CFrame.Angles(0, -yawAngleRelative + math.rad(-90), 0), elapsedTime / 0.1)

Something among those lines.

I just did some tests and it seems u are wrong

https://gyazo.com/58aba8d8838bdd551ebee32a222d9956.mp4

The code im using

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)

yeah that’s probably my fault, ill just add the radians onto that instead like this:

local x, prevY, z = part.CFrame:ToEulerAngles()
local positionalCFrame = CFrame.new(part.CFrame.p) * CFrame.Angles(x, lerp(prevY, -yawAngleRelative, 0.1) + (math.pi/2), z)

No, I am not wrong.

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.

now try putting the mouse right into the character in the middle and see what happens.