RaycastResult

RaycastResult

Overview

The RaycastResult represents the result of a raycast, which is used to detect intersections between a ray and objects in a 3D space.

Description

A raycast is a way to send out an invisible ray or line from a point in space, commonly used for collision detection, determining hit surfaces, or other spatial queries. The RaycastResult contains detailed information about the intersection or hit, such as the distance, hit position, surface normal, and the instance hit.

Properties

Distance

number

The distance from the origin of the raycast to the detected intersection point.

Code Samples

local Workspace = game:GetService("Workspace")

local Origin = Vector3.new(0, 50, -0)
local Direction = Vector3.new(0, 0, -3000)

local Result = Workspace:Raycast(Origin, Direction)
if Result then
   print("Hit distance:", Result.Distance)
end

Instance

Instance

The instance (e.g., a part or model) that the ray hit during the raycast.

Code Samples

local Workspace = game:GetService("Workspace")

local Origin = Vector3.new(0, 50, -0)
local Direction = Vector3.new(0, 0, -3000)

local Result = Workspace:Raycast(Origin, Direction)
if Result then
   print("Hit instance:", Result.Instance.Name)
end

Position

Vector3

The position in 3D space where the ray intersects with the object.

Code Samples

local Workspace = game:GetService("Workspace")

local Origin = Vector3.new(0, 50, -0)
local Direction = Vector3.new(0, 0, -3000)

local Result = Workspace:Raycast(Origin, Direction)
if Result then
   print("Hit position:", Result.Position)
end

Normal

Vector3

The surface normal at the hit point, indicating the direction perpendicular to the surface.

Code Samples

local Workspace = game:GetService("Workspace")

local Origin = Vector3.new(0, 50, -0)
local Direction = Vector3.new(0, 0, -3000)

local Result = Workspace:Raycast(Origin, Direction)
if Result then
   print("Surface normal:", Result.Normal)
end

Constructors

Methods

Last updated