Hello, in a game I am working on, you can place turret-like guns onto a character (a pillbaby lol), and then they should be able to pivot on one axis to look at the player’s mouse position, whilst also staying parallel to the character that it is welded to.
The main issue I am having is getting the turret to rotate all 360 degrees correctly, as I have tried limiting the rotation to strictly the “main” axis (the Z axis) while using CFrame.lookAt, but that produces a result where sometimes the Y axis rotation also plays a factor in the rotation, resulting in just using the Z produce incorrect results where it will not always follow your cursor. I know this is happening as I made the lookat freely rotate the turret as a test, and turning it behind the character caused it to also turn on the Y axis.
Furthermore, I need to make the turret stick to your body while maintaining its current rotation, but I do have some idea as to how I could do this, such as raycasting to get the normal or basing it off of the Torso’s rotation, since they will be correlated in some way; however, help with this as well would also be appreciated, and I can’t implement this yet until the first problem is sorted out.
Here is the current code; I have tried limiting the lookat property of the CFrame.lookAt to use the gun’s Z position, but as I said earlier, this produces some Y rotation that, without it, results in an incomplete rotation of the turret to lock onto the cursor. I have omitted parts that include rotating the turret to be aligned with the body if placed on the other side of the baby and just a couple of lines that turn on and off WeldConstraints when pivoting the gun, as these are not relevant.
local pos = gun:GetPivot().Position
local pointpos = mouse.Hit.Position
local lookat = CFrame.lookAt(pos, pointpos)
local x, y, z = lookat:ToOrientation()
local px, py, pz = gun:GetPivot():ToOrientation()
lookat = CFrame.new(lookat.Position) * CFrame.Angles(px, y, z) --setting y to py results in what i described above
gun:PivotTo(lookat)
I suspect that I will have to somehow combine both the Z and Y strictly from the lookat while still snapping it to the side of the baby. I have sent a screenshot of how one may place the turret on the character, with a rough drawing of the axis of rotation I am looking for.
Any help would be appreciated, thanks!


