Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Would One Set a Range for a Raycast?

Here's my code:

local rayOrigin = humrp.Position;
local rayDirection = direction.p;
		
local rayCast = Ray.new(rayOrigin, (rayDirection-rayOrigin).unit*20);
local part = workspace:FindPartOnRay(rayCast, character, false, true);

Where "rayOrigin" is the player's HumanoidRootPart Position and the rayDirection is "mouse.Hit.p"

I assumed the "unit*20" would set the range of the raycast to 20 but I guess I was very wrong haha. The raycast fires a beam that can go as far as it would like. How would I set the range of the ray to 30 studs?

Any help is greatly appreciated! Thanks!

1 answer

✓ Accepted answer

You used unit on the wrong part

local rayCast = Ray.new(rayOrigin, (rayDirection-rayOrigin)).Unit*20

Is what you're looking for, it didn't error before because you can use .unit on a Vector3 to change it's length to 1, in this case (rayDirection-rayOrigin) was that Vector3

1 comment

0

Getting the Unit of a Ray gives another Ray with a normalized direction (which would be identical to Ray.new(rayOrigin, (rayDirection-rayOrigin).Unit) ) Afterwards the ray is being multiplied by 20, which would error, so overall accepting this answer isn't correct?

This discussion is preserved as it was. New answers, comments, and votes are closed.