The mouse object has and I quote from the deb hub:
Mouse, by and large, has been superseded by UserInputService
which offers wider additional functionality for interacting with the mouse as well as other input types.
To do this you need to use the GetMouseLocation()
method from UserInputService, this will get the 2D position of the mouse relative to the top left corner of the screen.
So your code will look something like this:
local UserInputService = game:GetService("UserInputService")
local MousePos = UserInputService:GetMouseLocation()
But this will only ever get the mouse position once, when the script first runs, to get around this we will use BindToRenderStep
, because we are wanting the user input to have occured before we get the location and the camera not to have updated yet, the priority should be between 100-200
according to the render priority of render stepped.
The code should now look something like this:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local function foo()
local MousePos = UserInputService:GetMouseLocation()
end
RunService:BindToRenderStep("foo", 150, foo)
This is only getting the 2D position of the mouse, to turn this into a 3D position we can use the method ViewportPointToRay
from the Camera object, this returns a Ray object, from that Ray object we can get its origin and direction to make a CFrame!
The code should now look like this:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = game:GetService("Workspace").CurrentCamera
local function foo()
local MousePos = UserInputService:GetMouseLocation()
local ray = Camera:ViewportPointToRay(MousePos.X, MousePos.Y, 5) -- Mouse pos x,y, offset from camera
local CF = CFrame.new(ray.Origin, ray.Origin + ray.Direction)
end
RunService:BindToRenderStep("foo", 150, foo)
Now all we need to do is find the angle between the players Humanoid Root part and this CFrame. To do this we need to get the lookvector (direction the Humanoid Root Part is facing) and use the :Dot
method of Vector 3 to find the angle between the Humanoid Root Part and the CFrame from the ray. However, just doing this is not enough - it will output the opposite angle because the point we are getting the angle to is by the camera, so we need to move the point along the ray by just over the max camera zoom distance to get the correct angle.
That will look something like this:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = game:GetService("Workspace").CurrentCamera
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local function foo()
local Character = workspace:FindFirstChild(LocalPlayer.Name)
if not (Character) then return end
local MousePos = UserInputService:GetMouseLocation()
local ray = Camera:ViewportPointToRay(MousePos.X, MousePos.Y, 5) -- Mouse pos x,y, offset from camera
local rayEnd = ray.Origin + (LocalPlayer.CameraMaxZoomDistance + 5) * ray.Direction.unit
local CF = CFrame.new(rayEnd, rayEnd + ray.Direction)
local Facing = Character.HumanoidRootPart.CFrame.LookVector
local Angle = math.acos(Facing:Dot(CF.LookVector))
print(math.deg(Angle))
end
RunService:BindToRenderStep("foo", 150, foo)
Now all you need is a little more code to limit the rotation of the turret! We will check the angle, if it is too big or small then we will not move the turret otherwise we can!
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = game:GetService("Workspace").CurrentCamera
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Max = 100
local Min = -100
local function foo()
local Character = workspace:FindFirstChild(LocalPlayer.Name)
if not (Character) then return end
local MousePos = UserInputService:GetMouseLocation()
local ray = Camera:ViewportPointToRay(MousePos.X, MousePos.Y, 5) -- Mouse pos x,y, offset from camera
local rayEnd = ray.Origin + (LocalPlayer.CameraMaxZoomDistance + 5) * ray.Direction.unit
local CF = CFrame.new(rayEnd, rayEnd + ray.Direction)
local Facing = Character.HumanoidRootPart.CFrame.LookVector
local Angle = math.acos(Facing:Dot(CF.LookVector))
if Angle < Max and Angle > Min then -- Move turret
-- Move turret
end
end
RunService:BindToRenderStep("foo", 150, foo)