near sensor with python

please help me i’m very bad at python i always use logic only, but now i want to use python with a near sensors.
what i’m trying to do is make a cube do something ( play a sound for exemple ) when my player is near the cube ( exemple trigger distance = 5.00 )

noob code don’t work of course:

import bge

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    sens = cont.sensors['mySensor'] = Player
    NearSensor.distance = 5.00
    NearSensor.reset_distance = 6.00
    actu = cont.actuators['myActuator']

    if sens.positive:


change Player to ‘Player’

thanks but still don’t work

import bge

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    sens = cont.sensors['mySensor'] = 'Player'
    NearSensor.distance = 5.00
    NearSensor.reset_distance = 6.00
    actu = cont.actuators['myActuator']

    if sens.positive:


warning: near sensors are slow.

would gameObject.getDistanceTo() be any faster?

i think in upbge you can access the lod number which would be the fastest by a long shot.

NearSensor is not defined. Shoulld it be sens?

read the first line of my sig please.

Think about the timing and what you do when.

Your code gets be executed by a custom python controller C at frame f.
That means at least one connected sensor was triggering controller C withing frame f but before executing the controller.

Rule: sensors evaluate before controllers execute

You code searches for a connected sensor named “mySensor”. If it does not find one … it will result in error.

Then you reconfigure the sensor with certain parameters, overriding whatever was set before. This does not mean the sensor gets evaluated again! You basically prepare it for the next frame.

Then you check the status of the sensor “mySensor”, which is the result of the evaluation performed before this controller was triggered. It will not consider the configuration changes you applied earlier in this script.

Then you activate/deactivate a connected actuator called “myActuator”. This does not mean the actuator will run at that moment. It is just told it should activate/deactivate. The actuator will run later within frame f.

Rule: actuators will run when active and after all controllers executed. Activation and Deactivation signals are considered by the actuator. Deactivation does not necessarily make the actuator not active immediately (see action actuator in flipper mode).

Code review:
-> Check Cotaks’ post, you have an undefined variable (that should pop up in the console)
-> Have a look at the API documentation when you are unsure about the attributes (bge.types.KX_NearSensor.resetDistance)

-> Remove “main()” from your code. It makes the code look more complex without providing any benefits.

Compare your code to this:

import bge

controller = bge.logic.getCurrentController()
owner = controller.owner

mySensor = controller.sensors['mySensor'] = 'Player'
mySensor.distance = 5.00
mySensor.<b>resetDistance </b>= 6.00
myActuator = controller.actuators['myActuator']

if mySensor.positive:

Remark: I haven’t tested this. There might be other errors.

thank you Monster but still don’t work


near_test.blend (398 KB)

Alternatively you can test the length of two vectors it is cheaper than using the near sensor…with this you can test any vector distance, not just game objects. This is what I use.

import math
from mathutils import Vector#if you want to define custom vectors

if (player.worldPosition - my.worldPosition).length &lt; 5:

can you post a test .blend file with your method

Ok, you have encountered a bug (i think)

In your file, set the distances in the near brick.

  • now you see errors in the console.

yes does not exist, that is true to change the property of the near sensor you need to use sensor.propName.
Now that that is working, it still doesn’t work does it? that is due to the (u guess) bug of setting distances. If you set the distance in the near sensor it works. if you set near to 0.0 and set the distance in the script it fails. if you set both it works.

anyway rewrote your script into this

import bge

def near_activator(cont):
    own = cont.owner

    mySensor = cont.sensors['mySensor']
    mySensor.propName = 'player'
    mySensor.distance = 10.0
    mySensor.resetDistance = 20.0
    if mySensor.positive:
        own['prop'] += 1

define a function (module) near_activator.
functions have build in controller support, first passing command will be the controller, so to grab the controller we start with ‘def near_activator(cont)’ now in order to run this script, you need to rename your script from ‘Text’ to ‘’ now in the python brick you put it from script to module and type in ‘Text.near_activator’. Now you are using the function we have created.

You see i removed some code, all of it where not needed. You grab own, this is the object that runs the script, so we can change it’s property right away without using/activating more bricks and in this case a near sensor deactivates it self when reset distance is met, so deactivating it has no use and will result in positive state due to sensor check wil be within distance so it turns itself to True again.

thank you, but is there a way to set the near sensor in logic bricks to 0. ignore the value in logic bricks and use only the value in the script.

i want to use only the script, if we still using the logic bricks no need for a script :smiley:

Unless you want to reproduce the near sensor’s behavior with python code, you will always need the near sensor.

There is a nice GUI that allows you to configure the sensor quite easily. Why you try to avoid it?

As long as you do not need dynamic reconfiguration of the sensor (e.g. change the sensor range based on situation while in game) you make your development process more complicated than it needs to be.

The BGE is designed around logic bricks. They allow easy setup of static behavior which is typically 90% of the game. You can get dynamic behavior via custom python controllers. Be aware it is still a controller even when it can be used to act as sensor or as actuator.

Hint: If you want to become “python only” you can

  • write Blender scripts that generate logic bricks for you (obviously before the game starts) in conjunction with custom Python controllers.
  • switch to an according game engine that support Python only behavior description (like panda3d)

I would like to use the Near sensor for AI Steering in Speed Racer A Whole New World 2 can anyone advise me as to how i should go about with AI steering?


Maybe with navmesh and obstacles? I do not use navmesh though so?? maybe I should look into it…
here is a simple replaceMesh example using the vector distance as I stated earlier in this post.