Game Object: getDistanceTo

getDistanceTo(other)
  • Returns the distance to an object or point.

Return Type:

  • float number

other:

  • Type:  KX_GameObject  or  list [ x, y, z]  (a point)
Note:
  • Type: KX_GameObject

    Returns the distance to the game object object center

    Not the distance to the surface of the game object.
Sample Code

################## get the distance to another point
 

# 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 object named Box
box = objList["Box"]

# get the distance between them
distance = obj.getDistanceTo(box)