How can I get the player's mouse reverse position depending on the Humanoid Root Part position?

Hello! :wave:

I’m trying to get the mouse position of the player located to its reverse position depending on the Humanoid Root Part position. I know I will have to use ToObjectSpace()but I don’t know how and in which way.

Here is what I am trying to achieve:

Any helps appreciated! :grin:

(Mouse.Position - HRP.Position) + HRP.Position

You might have to flip the first two things, like (HRP.Position - Mouse.Position) + HRP.Position but it should be something like this.

Now that I thought about it it’s the second because the first makes no sense, so

(HRP.Position - Mouse.Position) + HRP.Position
2 Likes

Thank you so munch ! it’s working perfectly!

1 Like

Honestly there is only 1 issue with this, when the left side is not as high as the right side.
Instead I suggest to get the screen size, get the mouse position and then raycast from there.

Another issue is that it will get a floating point in any position, due to the y-offset.

The screensize can be gotten via CurrentCamera.ViewportSize, this returns a Vector2.
The mouse screen position can be gotten via mouse.x and mouse.y
Now to finally raycast you can use the reverse mouse position use camera:ViewportPointToRay()

For anyone who would want to do this more accurately I suggest to do the following:

local mouse = LocalPlayer:GetMouse()
local camera = workspace.CurrentCamera
local size = camera.ViewportSize

-- Raycast outward:
local ray = camera:ViewportPointToRay(size.X - mouse.x, size.Y - vector.Y, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {partToCheck, game.Players.LocalPlayer.Character}
local raycastResult = workspace:Raycast(ray.Origin, ray.Direction * 1000, raycastParams)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.