Hello I am trying to dot product sections around the player

I want to detect when the mouse is to the left of the player to the right of the player and above the player my attempt at this was;

local mouseRay = mouse.UnitRay;
	
local distance = (Vector3.new(mouse.X, mouse.Y, 0) - head.CFrame.p).Unit;

print(distance:Dot(head.CFrame.LookVector))

the problem here is the values go up when the mouse is on the left side and when the mouse goes to the top of the screen and I only wanna detect for the left and right value here is a video;

as you can see it increases when the mouse moves left or goes up and decreases when it goes right or down how can I separate these and not like one conjoined variable?

You can simply use Mouse position on screen and screen size (assuming that character is always at the center) to calculate whether the mouse is on the left or right

local PlayersService = game:GetService("Players")
local LocalPlayer = PlayersService.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
while task.wait() do
	if Mouse.X > Mouse.ViewSizeX/2 then
		print("Right")
	else
		print("Left")
	end
end

Now this would kinda work but is there a way to detect if its on the right or left side of the player character?

What if the cursor were on the character though?

divide the center by two and check if its on the right or left. If it’s on the center then just pick a random side for that edge case

local PlayersService = game:GetService("Players")
local LocalPlayer = PlayersService.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

Mouse.Move:Connect(function()
	if Mouse.X > Mouse.ViewSizeX/2 then
		print("Right")
	elseif Mouse.X < Mouse.ViewSizeX/2 then
		print("Left")
	else
		print("Center")
	end
end)
			
Mouse:GetPropertyChangedSignal("Target"):Connect(function()
	local Target = Mouse.Target
	local Character = LocalPlayer.Character
	for _, part in pairs(Character:GetChildren()) do
		if part.Name == Target.Name then
			--do code	
		end
	end
end)
1 Like

In that case you can use WorldToScreenPoint to get position of any bodypart on screen and then just compare the Mouse.X with the X of Vector3 returned by the function

2 Likes

Oh wait no way let me try this I forgot about this.

I think you need to use Camera | Roblox Creator Documentation instead, as Camera | Roblox Creator Documentation will ignore the gui inset. Also on the documentation page for Mouse | Roblox Creator Documentation, it says this:

When detecting changes in the mouse’s position on-screen, it is recommended that you use ContextActionService:BindAction with Enum.UserInputType.MouseMovement or UserInputService.InputChanged, which both describe the position of the mouse using the Position (a Vector3) of an InputObject, instead of using this and related properties.

I did this thanks for your reply Ive used it before and I dont wanna account for gui inset.

1 Like