Mouse.Hit.p not working correctly [SOLVED]

I want to be able to detect accurately where the player is aiming using the player’s mouse while both left and right mouse buttons are held.

While both the right and left mouse buttons are held consecutively (ie, right mouse button to aim and left mouse button to shoot continuously) the detection for the position of the mouse becomes innaccurate. (Note that the Mouse.Hit.p is accurate when shooting a single shot)
video example [Note: the output is showing the current Mouse.Hit.p]

I have been unsuccessful in finding solutions for this issue, so I decided to try my luck on this forum.

-- how I am currently getting the origin and direction for raycasting
    local Mouse = Player:GetMouse()
    local ORIGIN = Handle.Position
	local DIRECTION = (Mouse.Hit.p - ORIGIN).Unit
	print(Vector3.new(math.floor(Mouse.Hit.p.X), math.floor(Mouse.Hit.p.Y), math.floor(Mouse.Hit.p.Z)))
2 Likes

Mouse.Position should be enough for this, no?
A unit is just a normalized copy of the vector - one which has the same direction as the original but a magnitude of 1.

What happens if you only do this with Mouse.Position?

Removed the .Unit from local DIRECTION, but the error still occurs.
Or have I misunderstood?

What happens if you only use Mouse.Hit.p?

Replaced the local DIRECTION with Mouse.Hit.p, now it is completely inaccurate.
The local DIRECTION is being used for raycasting, was using .Unit so I could send the ray a specific amount of studs in the direction from the local ORIGIN to the Mouse.Hit.p

I’m assuming you tried this?

local DIRECTION = Mouse.Hit.p

You might want to print the DIRECTION without Vector3.new and math.floor (just not sure why you need those).

1 Like

That returns a CFrame value, it’s possible that the original poster either prefers to or needs a Vector3 value which is what the .p property extension returns.

Might be, not too familiar with this stuff, pretty sure you just need the mouse’s position though.

A CFrame value contains both positional & rotational components whereas a Vector3 is purely positional.

local Player = game.Players.LocalPlayer
local Handle = script.Parent
local Mouse = Player:GetMouse()
local ORIGIN = Handle.Position
local DIRECTION = (Mouse.Hit.p - ORIGIN).Unit
print(Vector3.new(math.floor(Mouse.Hit.p.X), math.floor(Mouse.Hit.p.Y), math.floor(Mouse.Hit.p.Z)))

You didn’t define “Player”. You also didn’t define “Handle”, I’ve set up a reference for both, you may need to change them depending on where stuff is located and/or if this is a server script you’ll need to get the player instance in some other way, such as a fired PlayerAdded event.

Sorry for leaving out variables, I should have mentioned of parts referencing undefined variables to be assumed already defined. The code above is just a small portion from the whole script which is relatively long and makes references to external factors in the game such as other scripts so I had thought it would be cumbersome to have someone read through the entire script.

I think I have worded what I was hoping to achieve poorly, I want to be able to get the mouse’s location in the 3D space more accurately. Mouse.Hit.p works fine when I am shooting slowly but if I shoot rapidly or automatically the Mouse.Hit.p seems to change slightly so that is annoyingly offset from where the mouse actually appears on screen. See the video linked above, notice how bullet holes form slightly offset from where the mouse is aiming.

Several people seemed to mention this, but I’m not sure if you answered it:

You are only using math.floor in the Print statement; not your actual calculation.
Right?

Can you show the actual calc, because, if your handle position is offset, and you are recalculating Hit.p using SOME MULTIPLE OF DIRECTION, it could be shooting exactly where you are telling it to shoot (Well, undoubtable it is).

The math.floor is only being used in the print, I’m using the actual coordinates for the calculations.
The reason I’m putting math.floor in the print is so I don’t have a super long string of numbers, when the mouse detects the wrong position it is always more than a 1 stud difference from the actual position of the mouse.
Roblox is down right now, so I can’t access studio, I will edit in the calculation as soon as I get in.

Looking at the video It appears the first shot is accurate but then the rest aren’t. When you create the bullet casings are you adding all the parts created to your blacklist?

2 Likes

Instead of using your method of getting the mouse position together with raycast use this more reliable method:

local RAY_DISTANCE: number = 1000 -- [Distance of ray];

local RaycastParams_: RaycastParams = RaycastParams.new()

RaycastParams_.FilterDescendantsInstances = { } -- [Hey, put the character here.]
RaycastParams_.FilterType = Enum.FilterType.Blacklist

local UserInput = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera

local workspace = workspace

local function MouseRaycast(raycastParams, distance): any
	local MOUSE_POSITION = UserInput:GetMouseLocation()
	local ViewportMouseRay = Camera:ViewportPointToRay(MOUSE_POSITION.X, MOUSE_POSITION.Y)
	return workspace:Raycast(ViewportMouseRay.Origin, ViewportMouseRay.Direction * (distance or RAY_DISTANCE), raycastParams)
end

local MouseRaycastReturned = MouseRaycast(RaycastParams_, RAY_DISTANCE)

print(MouseRaycastReturned.Position)
4 Likes

Yea, I have a debris folder for that stuff but the bullet hole was seemingly messing up the mouse’s hit position so I’ve just created an attachment at that point instead with the sound effects and particle effects.

Thanks, I tried this solution and it worked fine, but I have decided to just use attachments rather than another mouse detection.

it doens’t work if i try to click at the sky unlike mouse hit p