local Part = Instance.new("Part", workspace)
Part.Anchored = true
Part.CanCollide = false
local function GetOtherPlayer()
return game.Players:GetPlayers()[2]
end
do
game:GetService("RunService").RenderStepped:Connect(function()
local Character = GetOtherPlayer().Character
local Humanoid = Character and Character:FindFirstChild("Humanoid")
local RootPart = Character and Character:FindFirstChild("HumanoidRootPart")
if RootPart then
local p = CFrame.lookAt(workspace.CurrentCamera.CFrame.Position, RootPart.Position)
local x = -2 * game:GetService("UserInputService"):GetMouseLocation().X / workspace.CurrentCamera.ViewportSize.X + 1
local d = (workspace.CurrentCamera.CFrame.Position - RootPart.Position).magnitude
local n = d * math.tan(math.rad(workspace.CurrentCamera.MaxAxisFieldOfView)) * x * 2
print(x)
Part.Position = (p * CFrame.new(n, 0, -d)).Position
end
end)
end
here is a demo project of it working MouseAngle.rbxl (33.0 KB)
and this is the script
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local character = script.Parent
local rootPart = character.HumanoidRootPart
local camera = workspace.CurrentCamera
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
runService.Heartbeat:Connect(function(deltaTime)
local viewPoint, inViewport = camera:WorldToViewportPoint(rootPart.Position)
local mouse = userInputService:GetMouseLocation()
local mouseDirection = camera:ViewportPointToRay(mouse.X, viewPoint.Y).Direction
local partDirection = rootPart.Position - camera.CFrame.Position
local angle1 = math.acos(mouseDirection:Dot(partDirection.Unit))
local angle2 = math.pi / 2 - angle1
local length = partDirection.Magnitude * math.sin(angle1) / math.sin(angle2)
if mouse.X < viewPoint.X then length *= -1 end
local cFrame = camera.CFrame.Rotation + rootPart.Position
part.CFrame = cFrame * CFrame.new(length, 0, 0)
end)