Raycasting
Detect objects along a line.
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
Step-by-Step Guide
Follow these steps to work with raycasting in your Appley project:
Add rigidbody
Select the object in the hierarchy and add a rigidbody component for raycasting.
Configure collider
Set the collider shape (box, sphere, capsule, mesh) and size.
Set physics properties
Configure mass, friction, bounce, and other physics material properties.
Script forces
Use the physics API to apply forces, torques, and impulses from scripts.
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:
// 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:
[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
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
Physics is jittery/unstable
Fast objects tunnel through walls
Joints behave unpredictably
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.
Related Pages
Explore these related documentation pages:
Example Project
Download a complete example project demonstrating raycasting:
