# Can someone teach me about raycasting?

I have researched about it on YouTube, Dev Forum, and Dev Hub. I either didn’t understand, or they didn’t explain very well. Can someone help me on this?

What about ray casting don’t you understand? I think that is a better question to ask rather than asking to be taught raycasting because the developer hub article is not hard at all.

https://developer.roblox.com/en-us/articles/Raycasting

As @sjr04 said, you should always consult the dev hub first before creating a new post. And if you truly don’t understand, please try to show some sort of attempt at solving the problem.

That said, the most common misconception I see with raycasting is incorrectly defining the second argument, rayDirection. Screenshot from the dev hub article:

Often times people want to raycast from two defined points, and so throw those two vectors in the function but that’s not right.

Here’s an example of creating a directional vector from two defined points, raycasting from the player to a mouse click

``````local Player = game.Players.LocalPlayer
local origin = Player.Character.HumanoidRootPart.Position
local destination = Player:GetMouse().Hit.p
local direction = destination - origin -- create directional vector

local ray = Ray.new(origin, direction)
local hitPart, hitPosition = workspace:FindPartOnRay(ray, Player.Character) -- ignore the player
``````
1 Like

This explanation is a quick start:

A raycast is basically a ray, or a straight line, from one point to another. The reason we use rays is to detect if something is in the way or not.

A Ray is created from specifying the origin point(the start point) and the offset(the amount in Vector3 that it is moved). For example, if I use `Ray.new(Vector3.new(0, 0, 0), Vector3.new(1, 1, 1)` the origin(start) would be `0, 0, 0` and then end position would be `StartPostion + Vector3.new(1, 1, 1)`.

1 Like