Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

What is Raycasting and How Would I Use it?

Asked by 4 years ago

Okay so to be honest this isn't really a question. I am looking for someone to explain something to me.


Story

I have been seeing people using Raycasting for a while now for important things, and I decided it's time I figure out what it is. Let me introduce myself.


About Me

I am a moderate builder and an advanced scripter on Roblox. I have made no successful games to date, but hopefully I want to change that. I have been on Roblox for about 4 years, though my account was made only last year.


Conclusion

I want someone to explain what Raycasting is to me, and tell me how and when to use it.


Thanks in advance,

-PrismaticFruits

1
I think that is the best question layout I ever did. PrismaticFruits 842 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago

I'm not exactly sure why the large bio was needed, ya could have just asked about raycasting without it...

But ray casting is essentially what's in the name, casting a ray. What I mean by this is that you essentially create a ray, a sort of one directional line, which extends from an origin. The ray itself is made via the following:

local ray = Ray.new(Origin,Direction)

Now you might be wondering what's the purpose of a ray in this scenario? Well, it mostly revolves around one main feature, game.Workspace:FindPartOnRay(), which returns a part, a hit position, and a surface normal. By using this, one can essentially do checks to see whether an object is obstructing the line of the ray. If for example, you cast the ray from your camera to where the mouse is clicked, you can detect if there's anything between them, making it quite useful for projectiles for example in order to ensure that the player's gun has a direct line of sight to the mouse click position. Take the following for example:

local ray = Ray.new(game.Players.LocalPlayer.Character.Head.Position, Vector3.new(0,500,0))
local Part,HitPos,SurfaceNormal = game.Workspace:FindPartOnRay(ray,game.Players.LocalPlayer.Character)

if Part then
    print("The player is under an object")
end

The code block essentially casts a ray from the head 500 studs in an upwards direction, which then detects if a part exists between the head position and the endpoint. If it does, the player would essentially be under a roof of sorts, and you'd be able to detect like such.

0
So let's say I want to make a gun, how would I do that? I am not good with the mouse. PrismaticFruits 842 — 4y
Ad
Log in to vote
1
Answered by
Gojinhan 353 Moderation Voter
4 years ago
Edited 4 years ago

While everybody else is commenting good stuff, please just use workspace:Raycast() anyway. It's better practice as it's largely more recent, and especially so it helps take off work from the engine by not having to create and index a ray object, it's all one function.

Here's an example:

local mouse = game.Players.LocalPlayer:GetMouse()
local origin = Vector3.new(1,0,0)
local dir = mouse.Hit.Position
local length = 500

local params = RaycastParams.new()
params.FilterDescendantInstances = {game.Players.LocalPlayer.Character}

local raycastObject = Ray.new(origin, (dir - origin).Unit * length, params)
--raycastObject will be nil if the ray hits nothing, keep note of this.

if (raycastObject) then
    local hit = raycastObject.Instance
    local position = raycastObject.Position
    local surfaceNormal = raycastObject.Normal
    local mat = raycastObject.Material
end
0
How would I incorporate this into a gun? PrismaticFruits 842 — 4y
0
i pretty much just gave you that. Please make an attempt. Gojinhan 353 — 4y

Answer this question