Game Object: rayCast

rayCast(to, from, distance, property, face, xray, poly)
  • Casts a ray from one object/point to another object/point. 
     
  • Returns the first object hit that has the correct property.

Return type :

  • list [ GameObject, hit, normal, polygon]

to:

  • Type:  KX_GameObject or  Position  [ x, y, z ]  World coordinates.
from:
  • Type:  KX_GameObject or  Position  [ x, y, z ]  World coordinates.
distance:
  • Type:  float
  • distance can be negative (look behind the object rayCast is attached to).
  • distance = 0.0  (Ray stretches all the way to the other object center or position.)
property:
  • Property on an object that triggers the ray.
  • property = ""  (All properties trigger ray.)
face:
  • Type:  integer
  • 1 = return face normal
  • 0 = normal is oriented towards origin
x-ray:
  • Type:  integer
  • 1 = x-ray on.  Check behind objects that don't have property
  • 0 = x-ray off.  Only check first object hit.
poly:
  • Type:  int
  • 1 = return polygon (KX_PolyProxy)
  • 0 = don't return polygon (KX_PolyProxy)

GameObject: 
  • The game object that triggered the ray.
  • Type:  a KX_GameObject
hit:  
  • The position of the hit.  (The distance from the starting position of the ray.)
  • Type:  List [ x, y, z]
normal: 
  • The normal of the surface that the ray hit.  (The angle of the hit.)
  • Type: list [ x vector, y vector, z vector]
polygon:
  • Type:  KX_PolyProxy
Note:
  • Returns [ None, None, None, None] if nothing was hit.
  • The ray detects ghost objects.
  • The ray ignores objects with Object type: No collision
  • The ray ignores faces that don't have the collision flag enabled.
Sample Code

################### get the first object hit that has the correct property. 
    

# import bge
import bge

# get controller
cont = bge.logic.getCurrentController()

# get object that controller is attached to
obj = cont.owner

# get the current scene
scene = bge.logic.getCurrentScene()

# get a list of the objects in the scene
objList = scene.objects

# get the object named Cube
cube = objList["Cube"]

# raycast to game object named "Cube"
to = cube

# raycast from object python controller attached to
from = obj

# Ray stretches all the way to the other object center
distance = 0.0

# property on an other object that will trip the ray
property = "BlueTeam"

# return face normal
face = 1

# xray is enabled
xray = 1

# don't return polygon
poly = 0

# get GameObject, hit position and angle.
hit = obj.rayCast( to, from, distance, property, face, xray, poly)