Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Would One Set a Range for a Raycast?

Asked by 4 years ago

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

Log in to vote
1
Answered by
rower86 35
4 years ago
Edited 4 years ago

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

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? TimScript 80 — 4y
Ad

Answer this question