<!-- Page: https://rustic-game-engine.pages.dev/docs/api/physics-actions/queries/raycast -->

# Physics.raycast()

[Physics queries](https://rustic-game-engine.pages.dev/markdown/api/physics-actions/queries.md)

## Compatibility

**Supported languages:** C, C++, C#, Python, JavaScript, Lua 5.4, Luau, Java, PHP and HTML/inline JavaScript (binding spellings and return shapes differ).

Use a compatible native engine build and the language tools required by your binding. This does not establish browser/mobile export or release qualification for every OS.

The signature below is the shared reference notation; language examples show the actual binding syntax. Question marks mark optional arguments and explicit defaults are shown where recorded. Do not copy reference notation as a typed declaration.

See [Feature status and compatibility](https://rustic-game-engine.pages.dev/markdown/reference/compatibility.md) for API 1.0/1.1 and per-method restrictions. Engine release tags and Docs v0.1 are independent versions.

## Signature

`Physics.raycast(origin, direction, distance, ignore?)`

## Setup and usage

These engine-owned services work in Lua 5.4, Luau, JavaScript, Python, C, C++, C#, Java, PHP, and HTML inline JavaScript. Follow [Use gameplay actions](https://rustic-game-engine.pages.dev/markdown/guides/gameplay-actions.md) to create and attach a script, install external toolchains, and choose the correct language binding. PHP and HTML scripts belong under `ui/`; CSS alone runs no gameplay code.

The Calls section uses JavaScript signatures. The function examples below include native syntax for all ten script types, including SDK-specific alternatives and limitations. Attach a JavaScript Object Component script to the specified target and press **Play**. Globals are supplied by the engine. Schedule actions from `Start` or a callback, rather than at script top level. Native languages use typed options and their own builder conventions; see the guide's language bindings.

## Parameters

Origins and centers are finite three-component world vectors. Direction must be nonzero and is normalized by the engine. Ignore is an optional array of entity IDs in JavaScript; native query signatures differ.

## Return and timing

Raycast/sphereCast immediately return the nearest {entity, point, normal, distance} or null; overlap returns entity IDs.

## Execution behavior

Core write timing differs between immediate Lua host calls, deferred JavaScript/Web commands and external SDK query flushes. Scheduled actions advance with the scene clock; synchronous queries evaluate at the call. Follow [execution rules](https://rustic-game-engine.pages.dev/markdown/guides/scripting.md#when-mutations-take-effect) and the return/timing notes for this category.

## Language examples

Choose one snippet at a time and place it inside your language's Start/on_start callback. These are independent examples; each creates its own handles or builders. Follow [Gameplay language bindings](https://rustic-game-engine.pages.dev/markdown/guides/gameplay-actions/language-bindings.md) for imports and callback registration. Python must import the modules used in its snippet from rustic; C# uses using static Rustic; Java examples belong inside a Rustic subclass; PHP callbacks declare global $rustic. HTML snippets go inside an inline script in a ui/ HTML asset.

Use an anchored Part for transform and effect examples, an unanchored collidable Part for forces, and a perspective camera named Camera for camera examples. Create Target for follow examples. Audio examples require assets/music.ogg in the Play snapshot. Replace scene paths with those listed by Game.scene.List(). Animation playback and sequence-animation examples require the Reveal fixture on the [Animation page](https://rustic-game-engine.pages.dev/markdown/api/animation.md); marker callbacks also require its Halfway marker. Run clock controls separately; a paused clock cannot advance a timer.

C callback examples use these file-scope functions before on_start:

```c
static void on_done(void) { rustic.log("info", "done"); }
static void on_sample(RusticValue value) { (void)value; rustic.log("info", "sample received"); }
static void on_event(const RusticValue *args, size_t count) {
    (void)args; (void)count; rustic.log("info", "event received");
}
```

### Lua 5.4

```lua
local result = Physics.raycast({0,4,0}, {0,-1,0}, 10)
```

### JavaScript

```javascript
const result = Physics.raycast([0,4,0], [0,-1,0], 10);
```

### Python

```python
result = Physics.raycast([0,4,0], [0,-1,0], 10)
```

### C++

```cpp
auto result = Physics::raycast({0,4,0}, {0,-1,0}, 10);
```

### C#

```csharp
var result = Physics.raycast(new double[]{0,4,0}, new double[]{0,-1,0}, 10);
```

### Luau

```lua
local result = Physics.raycast({0,4,0}, {0,-1,0}, 10)
```

### C

```c
RusticHit result = Physics.raycast((RusticVector3){0,4,0}, (RusticVector3){0,-1,0}, 10);
```

### Java

```java
var result = Physics.raycast(new double[]{0,4,0}, new double[]{0,-1,0}, 10);
```

### PHP

```php
$result = Physics::raycast([0,4,0], [0,-1,0], 10);
```

### HTML / inline JS

```javascript
const result = Physics.raycast([0,4,0], [0,-1,0], 10);
```

## Shared Physics queries constraints

- Attach to a Part above an anchored collidable floor. The query reports the nearest hit.
- Queries use primitive world AABBs including parent transforms. SphereCast uses rounded sphere/box contacts; model meshes do not automatically acquire primitive colliders.

- See [Basic physics](https://rustic-game-engine.pages.dev/markdown/guides/physics.md) for collider setup, gravity and collision limits.
- Check script attachment, enabled state and the Console when an operation fails.

## Errors and debugging

Read the constraints above and the Console diagnostic. Invalid types, non-finite values, stale targets or unavailable native services can fail validation. A failed callback disables the behavior; a failed source reload keeps the last good instance. Exact error messages depend on the language adapter. See [Troubleshooting](https://rustic-game-engine.pages.dev/markdown/troubleshooting.md).

## Related APIs

- [Physics.sphereCast()](https://rustic-game-engine.pages.dev/markdown/api/physics-actions/queries/sphere-cast.md)
- [Physics.overlap()](https://rustic-game-engine.pages.dev/markdown/api/physics-actions/queries/overlap.md)

[All physics-actions/queries APIs](https://rustic-game-engine.pages.dev/markdown/api/physics-actions/queries.md)
