Raycast

Cast a ray from a point and detect the first collider hit.

Raycast

raycast(origin, direction=(0,0,1), distance=9999, traverse_target=scene, ignore=None, debug=False, color=color.white)

Casts a ray from origin along direction up to distance. Returns a HitInfo with details if it hits an entity with a collider.

Parameters

NameTypeDefaultDescription
originVec3Starting point of the ray.
directionVec3(0,0,1)Direction vector.
distancefloat9999Maximum length of the ray.
traverse_targetEntitysceneOnly entities under this parent will be considered.
ignorelist[Entity]NoneEntities to skip during collision checks.
debugboolFalseIf True, draw the ray line on screen.
colorColorcolor.whiteColor of the debug line if enabled.

Example

from ursina import *
from ursina.raycast import raycast

app = Ursina()

class Player(Entity):
    def update(self):
        dir = Vec3(held_keys['d'] - held_keys['a'], 0, held_keys['w'] - held_keys['s']).normalized()
        start = self.world_position + (self.up * .5)
        hit = raycast(start, dir, distance=.5, ignore=[self], debug=False)
        if not hit.hit:
            self.position += dir * 5 * time.dt

Player(model='cube', origin_y=-.5, color=color.orange)
wall = Entity(model='cube', collider='box', scale=(1,3,1), origin_y=-.5, color=color.azure)
app.run()