Fix orientation jumping

I have a script that makes a flashlight copy the rotation of the camera. I have also used a formula to make the movement smoother, which is shown in the code.

function tween(o,t,s)
	o = o + (t-o)*s
	return o
end

local x,y,z,lx,ly,lz= 0,0,0,0,0,0

runservice.RenderStepped:Connect(function()
	x,y,z = cam.CFrame:ToOrientation()
	print(y)
	
	lx = tween(lx,x,.25)
	ly = tween(ly,y,.25)
	lz = tween(lz,z,.25)
	
	fl:SetPrimaryPartCFrame(head.CFrame * CFrame.new(0,0,-1) )
	fl.fpart.Orientation = Vector3.new(math.deg(lx),math.deg(ly),math.deg(lz))
end)

However, the problem is that because of the way orientation works, when the Y rotation reaches a certain point it switches to a negative number. This causes the “tween” formula to jump. In the video below, the Y-axis rotation is printed in the console for better visualization

How can I prevent this jump?

this is my first time trying this specific thing so sorry if this is incredibly incorrect, try this

game:GetService("RunService").RenderStepped:Connect(function()
	local lv = cam.CFrame.LookVector
	x,y,z = lv.x,lv.y,lv.z

	lx = tween(lx,x,.25)
	ly = tween(ly,y,.25)
	lz = tween(lz,z,.25)
	fl.CFrame = CFrame.new(head.CFrame.p,head.CFrame.p+Vector3.new(lx,ly,lz)) * CFrame.new(0,0,-1)
end)

quick edit, here is a video demonstrating it

2 Likes

ty so much! it works perfectly now.
now im just wondering how different lookvector and orientation works?

I honestly never use orientation so I wouldn’t know, they look completely different tho

On another note, the gmod game you’re making looks very promising, I wonder what is down that funny dark corridor

edit: the jump seems to be because the orientation skips from 3 to -3, and your tweening tries to smoothly jump between those 2 axes (axiseseses) so it winds up going the long way, not sure

1 Like