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)
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.
What was the code when you tried lerping?
have you tried using tweenservice?
i feel like it would work good for this
no, it’s can broke Motor6d, so better use lerp
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?
Unfortunately it didn’t work when I did it.
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:
What’s going on?
Are you using a script or localscript? And are you using RunService? If so, what RunService event? (Stepped, Heartbeat, RenderStepped?)
I’m using a local script which is connected to a ModuleScript. I am using RunService yes, using RenderStepped.
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.
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
Sorry for the wait, I didn’t and still don’t have anything I can use roblox studio on atm
No worries, but do you have anything now?
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.
Sorry for the late reply, been doing school work. No worries about the late reply!
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.
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
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)
-- 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
I tried your method, however, unfortunately, the turret movement itself was choppy.
Do you have any solutions for the issue I described above?
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.
Thanks, I’ll try that out.