Mouse.hit is returning areas floating in the air?


#1

So I’m trying to make my weapons more reliable. Unfortunately, mouse.hit.p is totally unreliable in the first place. It seems to return points floating in the air. How can I avoid that?


#2

If your mouse is over nothing (or the object is really far away), mouse.Hit will just return the endpoint of the ray it used to check if the mouse is over anything. Among other things, this is necessary if you want to make a laser gun that still shoots at the direction of your mouse, but doesn’t hit anything. If you want to distinguish between when the mouse is hitting air and when it hits a part, you can check for mouse.Target == nil (air).


#3

I guess I can do that but it doesn’t like to go very far… If I use a custom ray, could I make it go any further?


#4

If you use a custom ray, the furthest it can go is 1000 studs. If your object is further than that, you would need to raycast multiple times (if it hits the end, continue with another raycast from there).


#5

Thanks for the info. So then the mouse only goes as far as 1,000 studs, I’m assuming?


#6

That is likely, but you’d have to test to be sure.


#7

Correct me if I’m wrong, but I thought the raycast functions were extended to have a maximum distance of 5,000 studs?


#8

Max raycast length is nominally 5000 studs. Usually.


#9

Making a custom raycast system instead is often a great idea, as you can use it to make the mouse drop though invisible and/or cancollide off objects.