I am trying to make a turret rotate towards the mouse ,I eventually want the gun to adjust on the Y axis also.
Client:
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Rep = game:GetService("ReplicatedStorage")
local RS = game:GetService("RunService")
local plr = Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local Event = Rep.Communications.Guns.aim
local function worldMouse(X,Y)
local ScreenRay = cam:ScreenPointToRay(X, Y)
local Ray = game.Workspace:Raycast(ScreenRay.Origin, ScreenRay.Direction * 1000)
if Ray then
print(Ray.Position)
return Ray
end
end
RS.Heartbeat:Connect(function()
local CursorPosition = UIS:GetMouseLocation()
Event:FireServer(worldMouse(CursorPosition.X, CursorPosition.Y))
end)
Server:
-- This is the code that should move it, but it doesnt.
local gun = workspace.FrontGun
local MyX = (mousepos.X - gun["ship gun"].Base.Attachment0.WorldPosition.X)
local MyY = (mousepos.Y - gun["ship gun"].Base.Attachment0.WorldPosition.Y)
local Theta = math.atan2(MyX, MyY)
gun["ship gun"].gun.Union.HingConstraint.TargetAngle = math.deg(Theta)```
[dsfsd23.rbxm|attachment](upload://6CdeDRnSkhYVq5nZWOX7h5JsayN.rbxm) (76.0 KB)