How to limit range of raycast mouse click on the void/sky

I want to get position of raycast when click on the sky within range of 100

1 Like

In most cases, the unit direction of a RayCast, which is a vector, is multiplied by some scalar constant (simply a number) to determine the length of the cast (if your system isn’t doing this, you may need to get the unit direction of the vector and then multiply it).

image

This principle is illustrated in the picture above. The length or “range” of vector A is increased by a factor of 2 whenever it is multiple by a scalar factor of 2.

With that same logic, you just need to reduce that constant to limit the effective range of your cast. Make sure to handle cases where the ray doesn’t intersect anything.

3 Likes

It’s like this : -1521.52001953125, 4684.74560546875, 8707.654296875 Yes it alot of number but i want to limit it at 100 stud

You can use a custom mouse function

That’s most likely the point of intersection rather than the vector you need. You need to get the direction of cast, normalize it (change its length to 1 while keeping its direction), and then multiply that vector by the maximum distance at which you want and use that vector in your final RayCast.

Normalizing a vector is easy: just multiply it by the reciprocal of its magnitude.
For example, the normalization of the vector <3, 4, 5> is given by 1 / 7.07 * <3, 4, 5>, where 1/7.07 is approximately equal to 1 / sqrt(3^2 + 4^2 + 5^2) (the magnitude of a vector). This is easy to calculate in ROBLOX – Vector3 instances have a Unit properly that gives you exactly this value.

This gives you the unit vector <0.424264, 0.565685, 0.707107>, whose square root of the sum of the square of its components, or its length, equals 1. You can use this unit vector in your RayCast operation: multiplying this unit vector by your maximum distance will give you a vector whose length is exactly that distance.

use player:GetMouse().Hit.Position and clamp it if you need to

I could just use Invisible Sphere to be the range, btw thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.