Mouse direction

Hello! I have an problem with the unit between mouse position and character primaryPart position. Basically the part’s position is not the same as the mouse position
Here the code: part2.Position = (mouseP - cameraPos).Unit * 150
Here the result: gyazo
any help?

1 Like

It seems you have to subtract the GuiService:GetGuiInset first.

mouseP = UserInputService:GetMouseLocation() - GuiService:GetGuiInset()

It returns a vector2, is there a way to return a vector3?

I suggest using Mouse.UnitRay.Direction, since the mouse wont always hit a target.

--mouseUR is the Mouse.UnitRay.Direction
part2.Position = mouseUR * 150

Yeah I tried that too but still the same result

local plr = game.Players.LocalPlayer
local inputservice = game:GetService("UserInputService")
local camera = game.Workspace.CurrentCamera

inputservice.InputBegan:Connect(function(input, typing)
    if typing then
    else
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            local mousex = plr:GetMouse().Hit.p.X
            local mousey = plr:GetMouse().Hit.p.Y
            print(mousex,mousey)
            local truepos = camera:ViewportPointToRay(mousex, mousey)
            print(truepos)
        end
    end
end)

you could use this in a local script to get the viewport location
and then use a remote event to place the part at the location

here you can see how i did my raycast function to get the mouse position

3 Likes

Hello I tried what you did but nothing changes :sad:
couldn’t it be some offset problem?

yer but i’m finding it hard to imagine where you have gone wrong because the Raycast function in my video will be giving you the correct mouse position in 3D space

1 Like

WAIT IT ACTUALLY WORKS, thank you so muchh