# raycast

Issues:

* This is currently in WIP, this means the feature can be buggy/non functional at times.&#x20;
* The current raycasting system is limited to detecting parts and does not support terrain or character interactions.
* Max distance the ray can reach is <mark style="color:yellow;">`6000`</mark> `studs`

<mark style="color:purple;">`table`</mark> -> <mark style="color:purple;">`result_table`</mark>

| Field       | Type                                         | Description                                                                 |
| ----------- | -------------------------------------------- | --------------------------------------------------------------------------- |
| `hit`       | <mark style="color:green;">`bool`</mark>     | `true` if the ray hit an object, `false` otherwise.                         |
| `distance`  | <mark style="color:green;">`number`</mark>   | The distance from `point_a` to the hit point.                               |
| `origin`    | <mark style="color:green;">`vector3`</mark>  | The starting position of the ray (`point_a`).                               |
| `hit_part`  | <mark style="color:green;">`instance`</mark> | The object or part that was hit by the ray. `nil` if hit is `false`.        |
| `hit_point` | <mark style="color:green;">`vector3`</mark>  | The position where the ray intersected the object. `nil` if hit is `false`. |

<mark style="color:purple;">`raycast`</mark> `send(`<mark style="color:green;">`point_a`</mark>, <mark style="color:green;">`point_b`</mark>, <mark style="color:green;">`ignore`</mark>`)` -> <mark style="color:purple;">result\_table</mark>

Casting an invisible ray from a <mark style="color:green;">`vector3`</mark> in a specified direction with a set length. After you caste the ray, you can detect if it intersects with a basepart.

| Name      | Type                                        | Description                                                |
| --------- | ------------------------------------------- | ---------------------------------------------------------- |
| `point_a` | <mark style="color:green;">`vector3`</mark> | The starting position of the ray.                          |
| `point_b` | <mark style="color:green;">`vector3`</mark> | The ending position of the ray.                            |
| `ignore`  | <mark style="color:green;">`table`</mark>   | A list of instances that should be ignored by the raycast. |

<mark style="color:purple;">`raycast`</mark> `setnext_ray_debug()` -> <mark style="color:purple;">void</mark>

Enables visual debugging for the next raycast.

* When this function is called, the next raycast performed will be displayed visually (e.g., as a line in the environment).
* This is useful for troubleshooting and verifying the raycast's behavior.

<div align="center"><figure><img src="https://2125949812-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FizHZiELUkSKcTCobLisu%2Fuploads%2FZsy5KzRxfqfNyHhWfDy0%2Fimage.png?alt=media&#x26;token=9731c0be-07ca-4e22-9037-24e389a004b3" alt="" width="563"><figcaption><p>An example of how the debug visuals will appear.</p></figcaption></figure></div>

<mark style="color:purple;">`raycast`</mark> `clear_logs()` -> <mark style="color:purple;">void</mark>

Clears all debug logs generated by `setnext_ray_debug`.

* Removes any visual representations or logged information created during debugging.

## Example

```lua
local workspace = game:get_service("Workspace")
local camera = workspace:find_first_child_class("Camera")

local cam_lookvector = camera.camera_lookvector
local cam_pos = camera.camera_position
local ray_target_pos = cam_pos:add(cam_lookvector:multiply_scalar(5000))
local random_part = workspace:find_first_child_class("Part")

raycast.setnext_ray_debug()
local results = raycast.send(cam_pos, ray_target_pos, { random_part }) -- will perform a raycast from the camera's position towards the camera's look-at direction
print(results)
print(results.origin.x, results.origin.y, results.origin.z) -- start position

if results.hit then
    print(results.hit_point.x, results.hit_point.y, results.hit_point.z) -- end position
    print(results.hit_part.name, results.hit_part.class_name)
end

wait(10000) -- 10 seconds

raycast.clear_logs()
```
