How to make objects initially invisible and visible when they're detected

Hi. I’am trying to make submarine game, working in UPBGE 0.3. I want to create some kind of echolocation system to detect enemies. Enemies should be first invisible to player. I can’t understand how to make them invisible and make appear when they detected. Can someone help and give any advice how to do this?

Like this?

Video Demonstration

Code Snippet

import bpy


def enemy_detected(self):
    # Detected enemy
    if self.sensors["Collision"].hitObjectList != "":
        for o in self.sensors["Collision"].hitObjectList:
            o.visible = True
    # Reset when enemy no longer detected
    if self.sensors["Collision"].status == 3:
        for o in LAST_DETECTED:
            o.visible = False


UPBGE_03_Echolocation.blend (138.6 KB)

1 Like

Well, that’s not the thing I want.
I’ll try to explain. I want it to be 3d and work same as real hydrolocation. Player activate radar - some kind of “ray” or “cone” goes to chosen direction (in 3d) from submarine/character that doesn’t move. When “ray/cone” meets invisible object it becomes visible to player.

So you want the character to be still an create an object that expands at some speed like radar? You can create an object and put it in a different layer and then create a keyboard sensor and an edit object actuator to add that object, a sphere or cone or whatever, when you press a key. The object will have an always sensor and a python controller. And then in python you can set the rate at which the object will scale. Not sure what the script is for scaling an object but i know it can be done. If you need one I can look through my files for one.
Then you just need a collision sensor on the objects you are toggling the visibility of to interact with a property value of the radar object and a visibility actuator.

Why doesn’t this one work for static objects?

Static objects are not influenced by collision. If you want them to be detectable, I suggest setting your objects as Dynamic and disabling movement for them.

1 Like

Thanks for your code, and your feedback!
Your advice helped me
However, I noticed some shortcomings.
For example, I have 2 objects that should disappear.
But if both objects (X/Y) hit the radar radius, and then the X object is outside the radar and the Y object is still in the radar, then both objects will be visible.
2023-01-26 23-30-14.mkv (4.1 MB)

The same goes for your file.