How do I use raycasting (Vector3 direction)

Okay so, I have recently been trying to use Raycasting, or better said just experiment a bit with it.
My main problem now is the direction the raycast is supposed to go to.
I just don’t understand how this is supposed to work. Why use Vector3 and now some angle in °.

I have been given this a few days ago in another post I made about making a weather system and just tried to modify / experiment with the direction it goes into but its just so confusing.

local ray = workspace:Raycast(player.Character.HumanoidRootPart.Position, Vector3.new(0, 100, 0), raycastParams);

I already looked on youtube, the documentation and some other websites, but nowhere I can find a solid explaination as to how Vector3 works here as replacement for °.

3 Likes

Picture the first Vector3 as the point the ray starts at, and the second Vector3 as the point the ray ends. the third parameter contains additional information for the ray.

The Raycast function provided by the workspace has 3 parameters I believe, each with their own way in how they function

  • The Starting Position (Vector3 preferably)

  • The Ending Position (Vector3 preferably)

  • The Params you created before creating your Raycast (Or RaycastParams.new())

Well, Vector3 is a way to describe the Position and the Orientation of an object. It is quite hard to get to grips with how Vector3 works with Direction. When it comes to direction in relation to a part it gets even harder.

If you need help understanding where your raycasts are firing I made a small function you can use in your own code should you wish to debug. :slight_smile:

local raycastView = Instance.new("Part")
raycastView.Anchored = true
raycastView.CanCollide = false
local function visRay(startPosition,direction,colour)
    local endPosition = startPosition + direction
    local midpoint = (startPosition + endPosition) /2
    local ray = raycastView:Clone()
    ray.Parent = workspace
    ray.Size = Vector3.new(0.2,0.2s,direction.Magnitude)
    ray.CFrame = CFrame.lookAt(midpoint,start)
    DebrisService:AddItem(ray,updateSpeed)
    --Ignore this if you do not care about the colour :)
    if colour == "red" then
    	ray.BrickColor =BrickColor.new("Really red")
    elseif colour == "green" then
    	ray.BrickColor =BrickColor.new("Bright green")
    elseif colour == "blue" then
    	ray.BrickColor = BrickColor.new("Bright blue")
    elseif colour == "white" then
		ray.BrickColor = BrickColor.new("Institutional white")
    end
    ray.Material = "Neon"
end

I found this piece of code helpful when I get confused about why my raycast don’t work. Hope this helps :slight_smile:

It actually did indeed help me to understand raycasts now.
But one question is still open now for me.
If I move around the raycast doesn’t follow with the changed direction.
So how can I do that?
https://csgo-russians.go-get-a.life/f9FhQg

workspace:Raycast(rayOrigin, rayDirection, RaycastParams)

The ray origin would simply be the point where the ray will start at, in your case you put the ray to start from the player’s humanoidrootpart.

The ray direction is the extent that your ray will detect things, so in your case, the part of the ray that you want to check at is the extent from the humanoid root part to the 0,100,0 vector.

By last there are the parameters for the ray, which I suppose you already know.

The reason of using Vector3 is because well, the Vector3 is a 3D point that involves position and direction (you can also search for Vector3 tutorials explaining about it on YouTube, which is not limited to roblox) meanwhile the other 3D point thing here is CFrame but we wouldnt need that since rays goes from a point to other so it doesnt need the rotation of a part (well, when you rotate a part the center will remain on the same point so it wont make any difference).

So well, if they used CFrame then it wouldnt make any difference than using Vector3 since the part center will remain on the same place even if you rotate it many times.

So to understand the direction you can use this, the blue vector “(end - start) if placed at origin” is the result of (endVector - startVector) but in the case of raycasting we use the rayOrigin, so the origin which normally is at 0,0,0 becomes the rayOrigin so with all this…

The ray becomes the (end - start) at the top between these 2 black vectors, since it simply moves the origin of the vector thats the result of (endVector - startVector) to the rayOrigin.

Now if you used (start - end), it would return the direction from the end to the start, and to simply explain this we can do the math now:

start = 0,6,6
end = 2,3,7

end - start = 2, -3, 1
Now lets put the origin at the start vector
0,6,6 + 2, -3, 1 = 2,3,7

It resulted on the end vector, so if the origin is 0,6,6 and the direction is the end vector - start vector then it will go from 0,6,6 till 2,3,7.

By the way, you are going to see this being used with Unit so many times, and if you wonder why they use it then there we go:

Unit returns a vector with the same direction but magnitude 1 (if you want the formula, its just each component divided by the magnitude of the vector) and the reason people uses it is because it remains the same direction and magnitude 1 so you can multiply it in order to give a “maximum distance”.

So in our case, if the rayDirection was (end - start).Unit*30 then the ray would start at the rayOrigin and face the direction from the rayOrigin to the rayDirection and would go to this direction by 30 units.

Maybe it wasnt good enough to understand so here is an example, lets say if you want to make a part that detects a player thats passing by its front and is in a distance of 10 units, for this we would do that:

--ill skip some parts and go straight to the ray
local start = part.Position
local direction = part.CFrame.LookVector -- This is a unit vector that starts from the part position and got the direction to where it is looking at, however as said before, the magnitude is 1 so its length is 1 and thats better for manipulation.

local rayResults = workspace:Raycast( start, direction*10, raycastParams)
-- By multiplying the unit vector by 10 you make its magnitude be 10 so it will detect whatever is in the front of the part at a distance of 10 units.

local humanoid = rayResults and rayResults.Instance and rayResults.Instance.Parent:FindFirstChild("Humanoid")

if humanoid then
    print(" Someone was detected! ")
end

And for the last, explaining the way you used.

That way is actually pretty straightforward to explain so it wouls basically work the same way as the one I showed before, so it would go from the start point to the end point, the reason why I showed it in different places is because the first one is pretty important to understand the Unit way.

I guess this is all then, I hope I helped.

11 Likes

The ray is made only once, after that if there are no more mentions for it then its cleared.

So for that you would have to do it every single time your character moves or you can use the RunService.Heartbeat event for it (the Heartbeat event fires every time the physics finishes being simulated)

Uh by that post I basically just meant something like updating the direction the ray fires everytime the player turns so its always facing correctly.
For example:
Vector3.new(100, 0, 0)

Image

RobloxStudioBeta_RlTYVaIcVh

As the direction, but when the player turns 180°, the value in the scrip also changes to -100 so it still fires into the same direction relatively to the player.

Update figured it out:
Vector3.new(direction.X*100, 100, direction.Z*100)

1 Like