The big problem with mouse position / raycasting

This is prevalent in many games and has been bothering me for a while, but basically, hats throw off the mouse position. I can only set mouse targetfilter to a single instance.

There are invisible parts (for accessories) in the character model throwing off the aim immensely. The issue is not that the ray registers the part as the hit, it’s that the mouse position is thrown off by it.

The good news, of course, is that there is a simple fix.

simplefix

My question: Why can’t hats be set to have size 0,0,0 by default, or not interfere with mouse target filter. It makes absolutely no sense for them to interfere with game physics in such a substantial way and I know MANY games that have not implemented methods I described to circumvent this (prison life 2, for example). I will refrain from naming the number of times I’ve failed to hit somebody with a ranged weapon because of this commonly made oversight.

1 Like

IIRC, it’s an artefact from back when you could drop hats in-game for someone else to wear. If they weren’t sized well they’d clip into the ground.

You might try using adding all Hats to a CollisionGroup that doesn’t collide with “Default”. According to the wiki, that should allow raycasts to ignore them.

1 Like

Yes. What I do in my game is I use Mouse.UnitRay in order to raycast.

Given Mouse.UnitRay:

local ray = Mouse.UnitRay
local origin, direction = ray.Origin, ray.Direction.Unit * 500 -- this is to make it extend a certain distance
local RP = RaycastParams.new() -- this is just RaycastParams
RP.CollisionGroup = "Raycast" -- if you make a collision group called "Raycast" that doesn't collide with accessories
local result = workspace:Raycast(origin, direction, RP)
local endPos = result and result.Position or Mouse.Hit.Position -- if no object is hit, then just use Mouse.Hit.Position
2 Likes

Very interesting. I’ll be sure to give this a try.