How to make object transparented when player approaches them based on their distance? 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)
1. In your material active (enable, check) Transparency checkbox:



2. Select the desire object

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


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 &lt; 1:
    own.meshes[0].materials[0].alpha = distance * 0.04
    print('alpha : ', own.meshes[0].materials[0].alpha)


Related

Python API (bge object material)

bge.types.KX_BlenderMaterial.alpha