We all start somewhere.
Raycasting is in the name itself; it casts a ray (an imaginary beam of light) in x direction. Roblox provides you with a beginner tutorial about raycasting. It contains everything you need to know.
For some sub-information there’s other tutorials on the developer forum, such as this one. I highly suggest you don’t dwell into complex systems; though I’m not stopping you. If you feel as though you could take the task, by all means, go ahead. However if you’re only beginning to comprehend raycasting, follow a basic pattern of “simple idea” → “simple problem” → “simple result”.
As I’ve stated above, raycasting is just an imaginary beam which travels in a certain direction. If it intersects with something, it’ll return a RaycastResult
containing some information about how the ray intersected, what the ray touched, the normal vector of the ray hit, etc… The entire list of components is listed in the official documentation.
I assume you’re talking about trajectory. There is some valuable information on the developer forum about that. Especially YouTube.
Into something more advanced? I’m confused on what you’re trying to achieve. Keep it simple, don’t overcomplicate raycasting; it’s a simple line, travelling in a given direction.
Some valuable information would be how to raycast from the mouse, or from a point on a screen. Methods such as ViewportPointToRay and ScreenPointToRay. If you wanted to raycast from the player’s Mouse
into the 3D world:
-- services
local userInputService = game:GetService("UserInputService")
-- variables
local currentCamera = workspace.CurrentCamera
-- functions
local function raycastFromMousePosition(origin: Vector3, raycastLength: number)
local mouseLocation = userInputService:GetMouseLocation()
local mousePositionX, mousePositionY = mouseLocation.X, mouseLocation.Y
local screenPointRay = currentCamera:ScreenPointToRay(mousePositionX, mousePositionY)
local direction = screenPointRay.Direction.Unit * raycastLength
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
return raycastResult
end
raycastFromMousePosition(Vector3.new(0, 30, 0), 1000)
Use RaycastParams
properly. If you have a raycast only meant to detect enemies, make sure you use Enum.RaycastFilterType.Include
, and have the FilterDescendantsInstances
table contain all the enemies. I’m sure the documentation iterates over some good practices.
Follow good tutorials. Whether they’re videos, forum posts or threads, as long as you’re learning something.
If you have any additional questions, feel free to ask.