Raycast to 3D Mouse Position is not accurate when firing in third person

What I want to achieve is pretty simple. An accurate raycast from origin (character head) to mouse hit position.
The raycast is perfectly accurate when using the gun in first person (aiming down sights), but is not accurate when firing the gun in third person.

I have tried using Mouse.hit, Mouse.hit.p, mouse.UnitRay.Direction, and Mouse.Hit.LookVector for raycast direction (randomly trying things out) to see if any of this would work, everything besides mouse.UnitRay.Direction has exhibited undesirable behavior. I am unsure what to do now.

here is the relevant bit of code for the raycast.

			local unitRayDirection = Mouse.UnitRay.Direction;
			local hitObject = workspace:Raycast(Character.Head.Position, unitRayDirection * maxRange, RayCastFilterObjects, RayCastGunFilter); -- just ignore the filters

You could try getting the direction using Mouse.hit.p and the characters head
Formula is:
Direction = HeadPos - MouseHit

The behaviour should be a ray coming from the players head and going towards the mouse’s hit position.

1 Like

Try changing




Wow. This works great. Thank you.