How to move part on one axis relative to my camera and somebody elses rootpart?

this is what i have so far

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

but the part’s position is not 100% accurate to my mouse as u can see below
https://gyazo.com/b2f03e8d03458f8158df401b751cff9f

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)

we first use Camera:WorldToViewportPoint to get where the part is on the screen

then we use UserInputService:GetMouseLocation to get the mouses position on the screen

then we use Camera:ViewportPointToRay to convert the mouses X and parts Y Screen position into a direction vector

then we also get the direction to the part from the camera

then we get the angle in radians between the 2 directions

and because we know we want the part to move 90 degrees (math.pi / 2) we can use that to workout the other angle of the triangle

then we can work out the distance we have to move the part with magnitude * sin(angle1) / sin(angle2)

if the mouse is on the left side of the part then we flip the length to a negative

and then make a cframe at the part and with the same rotation as the camera and then we can move the part using the length

1 Like

thanks so much bro

30 letterssssssssss