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

I was looking at raycasting and saw a line of code and idk what it does?

Hello, new to raycasting, but very confused with the end

local ray = Ray.new(script.Parent.Handle.CFrame.Position, (mouse.Hit.Position - script.Parent.Handle.CFrame.Position).unit * 300)

At the end the .unit * 300 part what does that do? First of all what does unit mean, second of all, what is * that supposed to do?

3 comments

0
  • is multiplication and unit is the Magnitude except from 0 to 1 or something like that
0

wdym 0 to 1?

0

The piece of code given to the second parameter of Ray.new() is simple vector math. It first calculates a direction of size 1 unit and multiplying it to a scalar value of 300. Making the length of that ray "300" units/studs long. https://devforum.roblox.com/t/learning-vector-math/97559/3

1 answer

Let's say you have a Vector3 with the value of (10,0,1), the unit would be a ratio of all components combined.

t = x + y + z

unit = Vector3.new(x / t, y / t, z / t)

Since 10 (x) + 0 (y) + 1 (z) is 11 t equals 11.

10 (x) / 11 (t) is 0.9090909...

1 (z) / 11 (t) is 0.0909090...

Therefor the unit is Vector3.new(0.9090909,0,0.09090909).

Unit can be used to set the distance of a raycast. The multiplication part is to change that distance.

2 comments

0

hmm im a little bit confused

0

yeah im still really confused with unit and *

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