near.distance doesn't work?


Ive been trying to make a good ol’ tower defense game, but I’ve hit a glitch. i need my range to be able to increase (I’m using near sensors…ugh :P) so I’ve been using the near.distance function to try and change it. The problem is it does nothing. I’ve used a test property with the corresponding get functions and it does change the distance value, but in testing the turret still reacts to the set value on the logic brick. Removing that value just causes it to not react at all. Any help or ideas are very much appreciated, thanks!

Interesting, doesn’t work here either, maybe its a glitch?

Anyway, is there a reason that your not using getDistanceTo()


i found the problem, the script only runs when the near sensor is activated or deactivated. but even using an always sensor set to true doesn’t stop the problem…

Is there a way to use that getDistanceTo with minimal performance hit? I’m afraid it’ll get to big once i start getting the enemies in, I can post the file if you want to look at it

Im afraid not…if you are saying that your sensor isn’t activating properly…then no.

There is a bug in the near sensor. The new distance is recogniced only when the sensor state changes to False. That means a near object leaves the old range.

As a workaround you can do following:

  • add an mesh object to an hidden layer layer
  • it needs at least one face
  • enable “Actor” to be recognised by the near sensor
  • enable “Ghost” to prevent collision with anything
  • enable “Invisible” to hide it.
  • if necessary add the near sensors property

when changing the near sensors distance you activate a

  • “Edit Object” actuator
  • on the object with the near sensor
  • in “Add Object” mode
  • adding the above object
  • with a livetime of 1

What will happen:
the frame you activate your script:

  • the near sensors distance is changed, but not recognised
  • the addObject is triggered
    the next frame:
  • a new object is within the range of the near activating the near
    the next frame:
  • the object disappeared - this sets the near to False (as long there is no other object) - let the new distance be recognised by the near sensor


  • let the above dummy object end itself with an always sensor (logic on the dummy object)
  • let your script add the dummy object with KX_Scene.addObject.
    This way the dummy should end within the next frame. I’m not sure about that.

I hope it helps