Raycast returns nil

I think i might just not understand raycasting, but every time i try to raycast it returns nil.

Here is an example:

print(workspace:Raycast(Vector3.new(10.205, 1155.83, 35.397), Vector3.new(10.205, 1155.83, 48.308)))

3 Likes

workspace:Raycast() accepts three parameters:

  • origin
  • direction
  • RaycastParams (optional)

You provided the function with the Position of the first block and the position of the second block. Here’s a simple drawing of where the ray is going.

image

We can take the direction according to the above picture.

local result = workspace:Raycast(partA.Position, partB.Position - partA.Position)
print(result)

Should you prefer to specify the magnitude of your directional vector yourself, you can multiply the distance with a unit vector (length of 1).

local result = workspace:Raycast(partA.Position, (partB.Position - partA.Position).Unit * 100)
print(result)
4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.