AppleyAppley Docs
Docs/Physics

Raycasting

Detect objects along a line.

13 min read Advanced Updated 2026-07-01 v2.0 ~38 min to complete

Introduction

Detect objects along a line. This page provides a complete guide to raycasting in Appley, covering everything from setup to advanced techniques.

Physics is a core area of Appley. Understanding raycasting will help you build better games and use the platform more effectively. This documentation is designed for advanced level users.

Requirements

Before working with raycasting, ensure you have the following:

  • An Appley project with a 3D scene
  • 3D models with colliders
  • Understanding of basic physics (mass, force, gravity)
  • Rigidbody components on objects

Estimated Completion Time

This guide takes approximately 38 minutes to complete. The estimated reading time is 13 minutes. Difficulty level: Advanced.

Step-by-Step Guide

Follow these steps to work with raycasting in your Appley project:

1

Add rigidbody

Select the object in the hierarchy and add a rigidbody component for raycasting.

2

Configure collider

Set the collider shape (box, sphere, capsule, mesh) and size.

3

Set physics properties

Configure mass, friction, bounce, and other physics material properties.

4

Script forces

Use the physics API to apply forces, torques, and impulses from scripts.

5

Test physics

Play the game and verify the physics behavior. Adjust properties as needed.

FluxScript Example

Below is an Appley Scripting (Flux Scripting) code example demonstrating raycasting:

Raycasting — Appley Scripting
// Physics — Raycasting
on start {
  ball = world.spawn("sphere")
  ball.setRigidbody(true)
  ball.setMass(2.0)
  ball.setBounce(0.8)
  ball.setFriction(0.3)
  ball.setPosition(0, 20, 0)
  ball.applyForce(vec3(0, 0, -100))
}

on collision(other) {
  if other.type == "floor" {
    vel = ball.getVelocity()
    ball.setVelocity(vel.x, -vel.y * 0.8, vel.z)
  }
  hit = physics.raycast(ball.position, vec3(0, -1, 0), 1.5)
  if hit != null ball.isGrounded = true
}

Node Script Example

The same logic can be implemented using Node Scripting. Here is the equivalent node graph:

Raycasting — Node Graph
[On Start] ──► [Spawn: sphere]
         ──► [Set Rigidbody: true, mass=2, bounce=0.8]
         ──► [Apply Force: (0,0,-100)]

[On Collision] ──► [Branch: "floor"]
            ──► [Get Velocity] ──► [Set Velocity: bounce]

Interchangeable Systems

FluxScript and Node Scripting compile to the same runtime. You can mix both in a single project — use whichever is best for each task.

Common Mistakes

Avoid these frequent mistakes when working with raycasting:

Making all colliders mesh-accurate

Use primitive colliders (box, sphere, capsule) for performance.

Not setting rigidbody mass correctly

Heavier objects should have proportionally higher mass values.

Using continuous collision detection everywhere

Use discrete mode for slow objects; continuous only for fast ones.

Creating physics objects in on update

Spawn rigidbodies in on start or via timers, never per-frame.

Troubleshooting

If you encounter issues with raycasting, try these solutions:

Objects fall through the floor
Ensure the floor has a collider. Check rigidbody is not kinematic.
Physics is jittery/unstable
Reduce rigidbody count. Check for overlapping colliders. Increase solver iterations.
Fast objects tunnel through walls
Enable continuous collision detection. Reduce speed or thicken colliders.
Joints behave unpredictably
Check anchor positions. Ensure connected bodies have appropriate mass ratios.

Performance Tips

Optimize your use of raycasting with these performance tips:

  • Use primitive colliders instead of mesh colliders for static objects.
  • Set up collision layers to control which objects interact with each other.
  • Use fixed timestep for consistent physics across different frame rates.
  • Put sleeping rigidbodies to rest when they are not moving to save CPU.

Explore these related documentation pages:

Example Project

Download a complete example project demonstrating raycasting: