Raycasting

An introduction to raycasting

by SaltyIceberg

Author Avatar

What is raycasting?

Raycasting is a way to cast a ray (a line that starts at a point) to go in a certain direction and returning whatever hits it (if any). If this tutorial is not useful, please go to Roblox's Developer Hub and search for "Raycasting."

Enough introductions! How do I begin?

  1. To begin, open Roblox Studio. If you do not have it, goto the Roblox site and click "Create."
  2. Then, click New > Baseplate
  3. After Studio loads, you should see the Explorer window on the right side. If you do not, go to View > Explorer.
  4. On the Explorer window, right-click on Workspace > Insert Object... > Script. This... inserts a script into your game.
  5. After that, you should see the IDE, or a text editor. If you do not, double left-click on the new script.
  6. There should be a single line of code on your screen that looks like the following:
print("Hello World!")

Go ahead and delete it. It is unnecessary for raycasting.

How do I raycast?

Now that you have successfully created a new script and currently have it open, it's time to code!

(The entire script will be at the end of this section.)

(Also, this is not a tutorial on variables, Instance.new() or properties.)

The blue part is the starting point of the ray, and the red part is the end of the ray.

First, we need to set-up variables and our parts.

-- Hey! This is a comment! Comments are a way to keep things neat and readable!
-- You DO NOT NEED TO COPY COMMENTS

local maxDistance = 500 -- Max distance our ray can go
local bluePart = Instance.new("Part", workspace) -- Spawns our blue part
bluePart.Position = Vector3.new(0, 0, 10)
bluePart.Anchored = true
bluePart.BrickColor = BrickColor.new("Really blue")

local redPart = Instance.new("Part") -- Spawns our red part
redPart.Position = Vector3.new(5, 0, 10)
redPart.Anchored = true
redPart.BrickColor = BrickColor.new("Really red")

Hmm... That's strange. Why is there a max distance?

Good question. Rays in geometry have an indefinite length. However in computers, they have a limit to how far out they can go.

Next, we need to create a ray and calculate the direction from the blue part to the red part.

local ray = Ray.new(
    bluePart.CFrame.Position, -- The starting point of the ray

    (redPart.CFrame.Position - bluePart.CFrame.Position).unit * maxDistance -- The direction the ray goes
)

Now we have created a new ray and put it in a variable. However, there is one problem: it will collide with the blue part!

local ignoreList = {bluePart} -- This will be what the ray will ignore

Whew... confusion and a catastrophe was avoided! Next, the ray will need to be casted!

We will forever cast and re-cast the same ray over and over again in a while loop. However, now is a good time to save.

Why save? Well, there is a chance you MAY MESS UP and FORGET WAIT()! If you do forget this, Studio will crash! :O

while true do
     local hit = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList) -- Casts a ray, sets the variable to whatever it hits



     wait(0.1) -- THIS IS REQUIRED!!!!!!!!!!!!
end

You just made your code cast a ray! Before running the game, you need to make sure your code does something when you hit the other part, if it even hits something at all. This is possible with an if statement.

(REMEMBER, raycasting sometimes returns nothing so the if statement needs to check if it exists.)

while true do
      local hit = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList) -- Casts a ray, sets the variable to whatever it hits
      -- hit could be nothing.
      if hit and hit == redPart then -- Checks if the part it hit is REAL and the part IT IS LOOKING FOR
            print("Hit :D")
      end

      wait(0.1) -- THIS IS REQUIRED!!!!!!!!!!!!
end

Amazing! The code is finished! Before playing the game, make sure the Output windoww is open (should be on the bottom of your screen). If it is not, go to View > Output. To play your game, go to Test > Play.

Conclusion

Now that your game is running, it should be printing "Hit :D" forever until you stop your game OR prevent the ray from hitting the red part.

You can test your raycast by walking in-between the parts. If you did, the printing should stop. Congratulations on raycasting! However, this is just the beginning of raycasting. Go to Roblox's Developer Hub and search for "Ray" to find everything elsethere are to raycasting.

I hope you enjoyed this tutorial! If you did or did not, please give a fair rating! Pardon any spelling or grammar related mistakes, please!

Completed Script

-- Section I

-- Hey! This is a comment! Comments are a way to keep things neat and readable!
-- You DO NOT NEED TO COPY COMMENTS


local maxDistance = 500 -- Max distance our ray can go
local bluePart = Instance.new("Part", workspace) -- Spawns our blue part
bluePart.Position = Vector3.new(0, 2, 10)
bluePart.Anchored = true
bluePart.BrickColor = BrickColor.new("Really blue")

local redPart = Instance.new("Part", workspace) -- Spawns our red part
redPart.Position = Vector3.new(5, 3, 15)
redPart.Anchored = true
redPart.BrickColor = BrickColor.new("Really red")

-- Section II

local ray = Ray.new(
      bluePart.CFrame.Position, -- The starting point of the ray

      (redPart.CFrame.Position - bluePart.CFrame.Position).unit * maxDistance -- The direction the ray goes
)

-- Section III

local ignoreList = {bluePart} -- This will be what the ray will ignore

-- Section IV & V

while true do
      local hit = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList) -- Casts a ray, sets the variable to whatever it hits
      -- hit could be nothing.
      if hit and hit == redPart then -- Checks if the part it hit is REAL and the part IT IS LOOKING FOR
            print("Hit :D")
      end

      wait(0.1) -- THIS IS REQUIRED!!!!!!!!!!!!
end

View in-game to comment, award, and more!