I have an horror game project, and I want to make an object disappear when player gets near them.

Of course, the disappeared object needs to have opacity step based on player's distance to the object.

So, Is there is anyway to make an object slowly disappear when player approaches them based on their distance? (Phyton suggestion allowed)

  • $\begingroup$ You can do this in a node shader using the camera node and extracting the Z distance. No idea if this is faster than a python solution or not, but it will work differently for really large objects (ie close parts of a big object are transparent while far parts are not) $\endgroup$
    – sdfgeoff
    May 8, 2017 at 19:53

1 Answer 1


1. In your material active (enable, check) Transparency checkbox:

set material

2. Select the desire object

3. In logic editor, add an always sensor connected to a pyton controller: enter image description here

4. script.py

from bge import logic
own = logic.getCurrentController().owner
player = logic.getCurrentScene().objects['Cube']
distance = own.getDistanceTo(player)
#if distance 
print('distance : ', distance*0.02)
if distance * 0.04 < 1:
    own.meshes[0].materials[0].alpha = distance * 0.04
    print('alpha : ', own.meshes[0].materials[0].alpha)


Python API (bge object material)


  • $\begingroup$ Glad to help :P $\endgroup$ May 8, 2017 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .