Smoothly rotating a Motor6D's C0

I am making a mouse-controlled turret which uses Motor6Ds and the Delta value of the player’s mouse. The movement works, however, it moves choppily and not smoothly. What should I do to fix this?

local horizontalMotor6d = turret["HorizontalMotor"]
local verticalMotor6d = turret["VerticalMotor"]

horizontalMotor6d.C0 *= CFrame.Angles(0, math.rad(horizontalDegrees), 0)
verticalMotor6d.C0 *= CFrame.Angles(math.rad(verticlalDegrees), 0, 0)

(extracted from the function to rotate the turret)

Here is a video of the issue: (sorry if it’s not very visible / has a low frame rate)
https://gyazo.com/1034eec8652a45b2683f338354c4ae32

I have already tried lerping it, however, this did not work at all and left the turret static and not following the joints I had made for it.

2 Likes

What was the code when you tried lerping?

2 Likes

have you tried using tweenservice?
i feel like it would work good for this

1 Like

no, it’s can broke Motor6d, so better use lerp

1 Like

The code was

horizontalMotor6d.C0 = horizontalMotor6d.C0:lerp(CFrame.Angles(0, math.rad(horizontalDegrees), 0), 0.54)
verticalMotor6d.C0 = verticalMotor6d.C0:lerp(CFrame.Angles(math.rad(verticalDegrees), 0, 0), 0.54)

I realise what the issue was now, I didn’t multiply the C0s by the angles in the lerp which meant it wouldn’t move. I’ve fixed it however it still doesn’t rotate smoothly? Could that be related to the mouse delta?

1 Like

Unfortunately it didn’t work when I did it.

Hi,

I updated the code. It now uses this to rotate:

self.horizontalMotor6d.C0 = self.horizontalMotor6d.C0:lerp(self.horizontalMotor6d.C0 * cFrameAngles(0, rad(horizontalDegrees), 0), 0.34)
self.verticalMotor6d.C0 = self.verticalMotor6d.C0:lerp(self.verticalMotor6d.C0 * cFrameAngles(rad(verticlalDegrees), 0, 0), 0.64)

(cFrameAngles / rad are localised functions, same as normal CFrame.Angles and math.rad)

Vertically, the rotation works perfectly. HOWEVER, when I move horizontally, the choppiness is AWFUL:
https://gyazo.com/88a2fee3768a6821a5646df3deccdf3a

What’s going on?

Are you using a script or localscript? And are you using RunService? If so, what RunService event? (Stepped, Heartbeat, RenderStepped?)

1 Like

I’m using a local script which is connected to a ModuleScript. I am using RunService yes, using RenderStepped.

1 Like

Give me a bit to do something I need to and then I’ll try remaking this and testing myself to see if I can get it to work, because right now I’m not sure what could be causing it.

1 Like

I think it may be related to the camera itself, as when I set the camera type to scriptable, there was no stuttering, not sure though

1 Like

Sorry for the wait, I didn’t and still don’t have anything I can use roblox studio on atm :frowning:

1 Like

No worries, but do you have anything now?

1 Like

I’ve been testing for 2 days with the camera a lot (not just for this) and I’m not sure how your system works for actually setting the CFrame but the way I set mine up worked fine, except instead of using mouse delta I just used the mouse position on the screen, but anyway that shouldn’t really matter much, how are you calculating the delta position?

Also sorry for the wait, I got side tracked.

1 Like

Sorry for the late reply, been doing school work. No worries about the late reply! :smiley:

I set the CFrame by lerping the new C0 and replacing the old C0 with the new lerped one.

I calculate the mouse delta position by using UserInputService:GetMouseDelta(). I used to use the mouse position on the screen and it worked smoothly, however the turret wouldn’t move around 360 degrees on the X or Y axis.

1 Like

Hm, when you tried getting the mouse delta yourself, what were you doing with it to turn the camera?
You could probably try to do something like this:
rotation += deltaX/screenSizeX*360
or if you’re using radians
rotation += deltaX/screenSizeX*2*math.pi

1 Like

This is how I turn the turret (I use a part that is welded to the turret and attach the camera to said part, so the camera’s CFrame isn’t changed directly when turning the turret)


		--local horizontalAngle, verticleAngle = getAngle(self.horizontalMotor6d.Part0.CFrame.LookVector, self.horizontalMotor6d.Part1.CFrame.LookVector, self.horizontalMotor6d.Part0.CFrame:VectorToWorldSpace(Vector3.new(0, 1, 0))), getAngle(self.verticalMotor6d.Part0.CFrame.LookVector, self.verticalMotor6d.Part1.CFrame.LookVector, -self.verticalMotor6d.Part0.CFrame.UpVector)


		self.horizontalMotor6d.C0 = self.horizontalMotor6d.C0:lerp(self.horizontalMotor6d.C0 * cFrameAngles(0, rad(horizontalDegrees), 0), 0.64)
		self.verticalMotor6d.C0 = self.verticalMotor6d.C0:lerp(self.verticalMotor6d.C0 * cFrameAngles(rad(verticalDegrees), 0, 0), 0.64)

	end

	-- gets the mouse delta positions on both axes, sends them to the turnTurret function
	function self:mouseTurn(mouseInputDelta: Vector2, deltaTime: number)

		local horizontalMovement = mouseInputDelta.X * 10 * deltaTime
		local verticalMovement = mouseInputDelta.Y * 3 * deltaTime 

		self:turnTurret(horizontalMovement, verticalMovement)

	end

I tried your method, however, unfortunately, the turret movement itself was choppy.

1 Like

Do you have any solutions for the issue I described above?

1 Like

Oh, I swore I responded to that so sorry.

Okay so, I notice that you’re multiplying the mouse delta by 10 which may be causing it to lose some precision in it, instead, maybe you should multiply the CFrame turn by 10 some way.

1 Like

No worries.

Thanks, I’ll try that out.

1 Like