How do you get the Vector3 position of the mouse?

How do you get the vector3 of the mouse?

I’ve been making a game called throwing simulator (link below)
https://www.roblox.com/games/6869371095/100-VISITS-Throwing-Simulator
If you got to the shop and buy the first rock then throw it, it always goes to the center of whatever your mouse is on. I want it to just go straight where the mouse position is. I have found another topic on this but it did not answer my question. Which leaves us to my question:

How do you convert the mouse position to vector3?

1 Like
--LocalScript
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local pos = mouse.Hit.Position -- 'hit' is a CFrame

To note - the Hit point will vary depending on any object the mouse is over at the time.
If you wish to ignore this you can use the mouse’ TargetFilter property and set it to the object(s) you wish to ignore.

mouse.TargetFilter = workspace -- ignore everything
8 Likes

That code is what I am currently doing. I want the rock to go exactly where the mouse is, that code takes it to the center.

Show us your code, and a picture of the actual issue.

Is the Rock Anchored?

If so you probably just need to offset the position by the size of the rock.

local pos = mouse.Hit.Position + Vector3.new(0, rock.Size.Y/2, 0)

-- Position is a Vector3 by the way

I can’t really show my code, I am on mobile and my code is on my computer. I do know that position is a vector3. I guess I could have worded the title different, I need it to go to the exact vector3 position as the mouse. Your code is currently what I am doing. Though I have not tried the size offset. You can see the issue in the game when you buy a rock, whatever you click it goes to the center of it.


I managed to get my code, any ideas on how to fix it?

Sorry for the bad quality picture…

1 Like

Your “Goal” is mouseTarget.Position so it is just getting the position (origin) of the part you click on, which is why it goes the center of the part… You should do what @WingItMan said which was mouse.Hit.Position.

It worked! Thanks for the advice, sorry @WingItMan. I thought Hit.Position was the same as Target.Position