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

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

Asked by 5 years ago

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?

0
* is multiplication and unit is the Magnitude except from 0 to 1 or something like that greatneil80 2647 — 5y
0
wdym 0 to 1? bluemountainlion 56 — 5y
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 xPolarium 1388 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
hmm im a little bit confused bluemountainlion 56 — 5y
0
yeah im still really confused with unit and * bluemountainlion 56 — 5y
Ad

Answer this question