Problems with my script need help

i want the objects get transperency if there between the cam and the player… look at this

import bge
import GameLogic

cont = GameLogic.getCurrentController()
own = cont.owner
scene = GameLogic.getCurrentScene()
sens = cont.sensors[‘CollideTrans’]

if sens.positive:
for obj in scene.objects:
if “Fade” in obj.name:
obj.color = [ 0.0, 4.0, 0.0, 1.0]


i need help to fix it every object that have the prop. Fade in it get Transperency…
hope for help

This is not going to work,

first of all… gamelogic is very outdated, use bge.logic instead.

second, what you do here is waiting for a sensor to get positive then you loop trough all scene objects and then you set the color of the object that has a property called Fade.

So how to fix this you ask?
You should cast a ray from the camera in the direction you are looking at, then when the ray gets positive, you check if the ray,hitObject has the property, then you set the color.

#edit
This should do, collision changed to a ray sensor
run this in module mode, always (true) - python(module) -> scriptname.fade_item_in_front_of_camera


def fade_item_in_front_of_camera(cont):


    own = cont.owner
    scene = own.scene
    
    #camera ray sensor (change the name)
    ray = cont.sensors['ray_sensor']




    if ray.positive:
        
        #grab the object hit by the ray
        hit_obj = ray.hitObject
        
        #a compare distance, set however you like
        distance_check = 10    
        
        #get the current distance to the object
        distance = own.getDistanceTo(hit_obj)
        
        #check if distance is smaller then our check
        if distance <= distance_check:
            
            #check if the hit_obj has a property called Fade
            if "Fade" in hit_obj:
                #set alpha/color
                hit_obj.color = [ 0.0, 0.0, 0.0, 0.0]
        else:
            #return alpha/color
            hit_obj.color = [ 1.0, 1.0, 1.0, 1.0]
                    

i would hardly call that a “fade” , more like swift change from opaque to full transparent.

It works but the else funktion that the trensparency go back to full color and alpah dont get ???

i would hardly call that a “fade” , more like swift change from opaque to full transparent.

true, its a toggle, nothing fades, i used the wrong naming there, my bad. But if you use an animation instead of changing the color then you can create a fade in/out, so haha i thought in advance ^^

It works but the else funktion that the trensparency go back to full color and alpah dont get ???

what distance did you set in the ray sensor itself? make it longer(double) then the distance the scripts checks.

And does the console give any errors? I did not test the code.

the range is 28.000 and the checker value i do it to 26.000
it give no syntax errors but the else will not work …

see this file its a testing

Attachments

blendoutobjText.blend (651 KB)

i just came home and looked at my code, it wont work indeed. it never would why?

  1. you need to be out of ray range AND when you are hit_object is not defined so we need to tackle this an other way.

Solution 2:
add the objects into a list, when ray is not positive loop trough list and set objects back visible.

well it is nice but the player get transperency to thats why i wont the property


from bge import render


render.showMouse(True)


def hide_item_in_front_of_camera(cont):


    own = cont.owner
    scene = own.scene


    ray = cont.sensors['Ray']


    if ray.positive:
        
        hit_obj = ray.hitObject
        
        if 'Fade' in hit_obj:
            hit_obj.color = [ 0.0, 0.0, 0.0, 0.0]
            own['last_hidden'] = hit_obj
        
    elif own['last_hidden']:        
        own['last_hidden'].color = [ 1.0, 1.0, 1.0, 1.0]
        own['last_hidden'] = ''


use this instead of the above code, its way better.
And also added the property for you

a working blend with this code:
hide obj in front of camera2.blend (481 KB)

Thank you for help to get this one ^^

NP :slight_smile:

(edit first post-> advanced-> set prefix to solved)