-1

Imagine I have a block in the middle of a 3-dimensional room. I know the position of that block, and I know the direction of the ray I want to cast.

How would the direction be represented in data? Degrees? Vectors? Magnitude? I honestly have no clue.

How would I set it up in Lua? (Following this rough outline:)

local ray = Ray.new( --origin(Vector3.new(x,y,z)), -- direction )

ie: what do I put for --direction?

Andrew
  • 15
  • 6

1 Answers1

1

I'm assuming this is Rbx.Lua from the looks of it.

Ray.new() takes two parameters. A CFrame direction and a distance in studs. Like so

Ray.new( Direction, Distance)

for instance the following code

local cf = CFrame.new(0, 1, 0)
local ray = Ray.new(cf, 10)

would cast a ray along the Y axis for 10 length units.

now, from the sound of it you have a block and you want the direction the front face of the block is facing. Roblox provides a property for this and it's called 'LookVector'. Thus you can do the following to cast a ray in the direction that a block is facing.

local cf = Part.CFrame.lookVector
local ray = Ray.new(cf, 10) 

Edit:

Here's another example, for raycasting between two Roblox Vector3 values.

local point1 = Vector3.new(0,0,0)
local point2 = Vector3.new(0, 1, 0)
local cf = CFrame.new(point1, point2)
local ray = Ray.new(cf, 10)

This will cast a ray from point 1 in the direction of point 2

Henry
  • 149
  • 1
  • 3
  • 12