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)
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?
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’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?
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)
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.
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.