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.