Mouse Controlled Turret with Base Turning on the X- Axis and the Elevator on the Y - Axis but dragged with the Base

Hello, I would like to make a turret which rotates with the player’s mouse depending on where the cursor is in the 3D space, which I’ve already done by firing a ScreenToPoint Raycast in the game which returns me the position of where the ray hit to my main function.

The function from there on sends it over to another script (server script), through a remote event which feeds the information to the Hinges (I am using hinge constraints), which later on turns them depending on the magnitude between the parts, the angle at which it’s facing and so on and so on.

The issue is that I can’t seem to make the elevator (which is supposed to only move up and down on the Y Axis), to rotate together with the base X-Axis part, it looks and angles towards the part correctly but doesn’t rotate.

I’ve searched for solution in a lot of different places, been sitting on the problem for a couple of days now and still can’t come up with an appropriate solution. I have tried align orientation for the parts and completely forget about hinges but for align orientation you need an instance and a ray isn’t an instance therefore it has no CFrame for me to grab to feed into the align orientation feature.

Here’s the code for the work so far:

-- Client Script to register the position of the cursor in 3d space
function mousePositionGetter()
	local cursorPosition = UserInputService:GetMouseLocation()
	MouseMoveEvent:FireServer(worldMouse(cursorPosition.X, cursorPosition.Y))
end

function worldMouse(X, Y)
	local screenRay = camera:ScreenPointToRay(X, Y)
	local ray = game.Workspace:Raycast(screenRay.Origin, screenRay.Direction * 5000)
	if ray then
		return ray.Position
	end
end

runService.Heartbeat:Connect(mousePositionGetter)
-- Server script to move the turret
function MoveRadarMouse(plr, rayMousePosition)
	if (rayMousePosition - RadarLeg.RadarHead.Position).Magnitude > 10 then
		-- Original Hinge Movement Script
		local myX = (rayMousePosition.X - RadarLeg.RadarHead.Attachment1LeftRight.WorldPosition.X)
		local myY = (rayMousePosition.Y - RadarLeg.RadarHead.Attachment1LeftRight.WorldPosition.Y)
		local myZ = (rayMousePosition.Z - RadarLeg.RadarHead.Attachment1LeftRight.WorldPosition.Z)
		local thetaX = math.atan2(myX, myZ)
		local thetaY = math.atan2(myX, myY)
		RadarLeg.HingeConstraintLeftRight.TargetAngle = math.deg(thetaX) + 90 -- Focus here tomorrow
		RadarLeg.HingeConstraintUpDown.TargetAngle = math.deg(thetaY) + 90
		-- The Align Orientation Attempt 
        -- The part below isn't always in the script was for testing purposes
		local rayPart = Instance.new("Part")
		rayPart.Name = "Temp"
		rayPart.Parent = game.Workspace
		rayPart.Anchored = true
		rayPart.Position = rayMousePosition
		local attachment0 = Instance.new("Attachment")
		attachment0.Parent = RadarLeg.RadarHead
		local attachment1 = Instance.new("Attachment")
		attachment1.Parent = rayPart	
		local alignOrientation = Instance.new("AlignOrientation")
		alignOrientation.Parent = RadarLeg.RadarHead
		alignOrientation.Attachment0 = attachment0
		alignOrientation.Attachment1 = attachment1
		alignOrientation.Mode = 1
		alignOrientation.RigidityEnabled = true
	end
end

If you have any ideas, or possible solutions I could try, I’ll be more then glad to read them here.

Here’s the turret itself. (Supposed to be a radar rangefinder), point is, two different hinges,
image


Here’s how it’s built:
image


Here’s the gameplay.
https://imgur.com/FyE7WDS


All of the hinges use Servo Actuator Type for moving, I can send pictures of the hinge orientation if necessary.

Here is the function:

Here is the diagram of how it works, the only difference is the X and Z axis is swapped because of how Hinge Constraints works and which axis they use:

The first function seems to be working pretty well, except that it’s a little 90 degrees to the right and by subtracting 90 degrees to it seems to fix the problem, maybe there’s another solution to this tho. Except for that your function essentially does what I’m able to replicate already, what I’m looking is how to make the bottom part (which is shown in the game), to turn on the Y Axis (Up and Down) on the Pitch angle instead of the yaw.

– But I also want it to turn together with the Yaw Angle by having the other hinge move it, can a hinge move another hinge or would they break?

Yes they can move each other and the function will calculate the correct angle based on relative positon.

This angle calculation is based on world position as it doesnt have to Object Space in it.

Right, but what I’m looking for is how to calculate the Yaw Angle for the white part in the image, to rotate up and down.

Still looking for some help if anyone got any other ideas regarding how I could find a solution to this problem. I want to figure out how I can control the pitch turn of the turret and make the other hinge turn as well together on the yaw.

Are you looking for a effect like the videos below which also pitch and yaw?

If so I think you have a rigging problem like making the pole on the base rotate (RadarLeg joined with the RadarBase) instead of the radar elevator perhaps.

Wow that’s exactly what I’m looking for, the way you’re making the object rotate is exactly what I need.

I want my base to rotate individually and then the other part to rotate together with it (like the gun in your video), to move on the pitch to face the object. The object in my context is my mouse.

So what should I do to make that happen? Something I can do usinghinges?

Also, what do you mean by rigging problem? Are you telling me I should make the pole rotate instead of the head of the turret? The base itself and the leg don’t turn.

Yeah the hinges should be in different axises.

One should be pointing horizontal to rotate along the local X axis, this makes it rotate up and down to shoot planes “Pitch”. Labeled as 1 on the figure.

One should be pointing vertical on the Y axis which is the base located on the seat to make it “Yaw”. I labeled as 2 on the figure.

I remember seeing this model on the dev forum but here is the link credits to the owner:

https://create.roblox.com/marketplace/asset/7131701158/TurretModelTest

Wow this seems to work! Exactly what I’m looking for! Thank you!

The hinges that I made seem to be accurate and move correctly, but there’s a slight problem, my calculations for finding the Pitch seem to be inaccurate.

This is what I am trying to do to find the pitch:

	local myX = (rayMousePosition.X - RadarLeg.Attachment0UpDown.WorldPosition.X)
	local myY = (rayMousePosition.Y - RadarLeg.Attachment0UpDown.WorldPosition.Y)
	local thetaY = math.atan2(myX, myY)
	RadarLeg.HingeConstraintUpDown.TargetAngle = math.deg(thetaY)

The center part seems to be looking down for some reason, I tried edding some degrees to it at the final s for the current angle but it doesn’t seem to work.

image

But then when it’s close then it’s facing correctly?

image

Another example of how it looks:

image

Any ideas regarding what should I do?

Try using my function

I used your function for yaw, but not pitch, wouldn’t it have to be slightly different?

Does the AngularResponsiveness matter in this case?
image

I tried using a Roof Pitch Formula to try and calculate the angle at which to face but it doesn’t seem to work, tried doing an inverse calculations of that but it also doesn’t seem to work.

Still kinda stuck on this question, looking for ideas in regards to how I could solve this problem

Is anything welded to the base?

Nope, I don’t think so, as far as I’m looking right now I don’t think anything is welded to the base. Only the leg.
image

Fixed, just played around with the hinge, it works but not very accurate.