Is `mouse.Hit.p.unit` innacurate?

I am currently making an article on dragging objects with your mouse, and while working on that I stumbled across this problem.

mouse.Target.Position = mouse.Hit.p.unit * constant

The part seems to not go the same direction as the mouse’s position, and what’s interesting, if I try another way to find the direction of the mouse other than .unit, for instance the .UnitRay property of Mouse, that one works fine.

https://i.imgur.com/xhfoeZ6.gif

I don’t get why this is happening, anybody got an explanation?

1 Like

The Hit CFrame will change if the mouse “hits” something, and thus is not going to necessarily represent a perfect orbital pattern around the camera. It’s going to cause weird issues. Plus, you need to offset your part’s position based on the camera’s position too. Without doing so, it will appear to orbit around the point of origin (0, 0, 0).

Therefore, it’s much better to just get the mouse’s UnitRay as you did in your second example, because it is not going to be affected by objects in the scene.


To demonstrate: Your first example is kind of “fixed” if you offset from the camera’s position:

local cam = workspace.CurrentCamera
mouse.Target.Position = cam.CFrame.Position + (mouse.Hit.Position.Unit * constant)

However, if you point your mouse at the baseplate, you will notice it get all weird. It will only work if you point your mouse at the sky. Again, this is because the mouse’s raycasting has been obstructed and is returning the position of the obstruction as the Hit property. Not good for what you’re trying to do.

So yeah, just stick with mouse.UnitRay:

local cam = workspace.CurrentCamera
mouse.Target.Position = cam.CFrame.Position + (mouse.UnitRay.Direction * constant)
6 Likes

Thanks for the answer! I think I got it, I will cover this and I’m also probably going to show how to do this in multiple ways such as messing the the camera’s cframe as you tried

Hay crazyman sorry for interrupting again but I also was going to ask what is the camera’s postion origin, or is it like global and it acts up like an origin?

Sorry, not entirely sure what you’re asking.

The camera’s position is the point from which you as the player are seeing.

Yeah sorry, I just did some tests and I figured it out it, when I read it now it doesn’t make any sense as well
thanks again!