On and off shader

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By szccornish

func _process(delta):
— var bodies = get_overlapping_bodies()
----for body in bodies:
-------if (body.get_name() == PLAYERNAME):
----------stairs.material.set(“shader_param/width”, 0.5)
------else:
--------if (body.get_name() != PLAYERNAME):
-------------stairs.material.set(“shader_param/width”, 0.0)

hello can somebody help me? im trying to make the shader go “on” and “off” if the player get in the area2D.
until the statment “if” is work fine. but when i but the “else” statement. it doesn’t work at all for both.
i wonder if someone have an insight?

nvm i find a way… i use body_exited instade of :
--------if (body.getname() != PLAYERNAME):
-------------stairs.material.set(“shader_param/width”, 0.0)

szccornish | 2021-06-02 15:02

:bust_in_silhouette: Reply From: timothybrentwood
func physics_process(delta):
    var bodies = get_overlapping_bodies()
    var player_inside = false
    for body in bodies:
        if (body.get_name() == PLAYERNAME):
            player_inside = true
            break
    if player_inside:
        stairs.material.set("shaderparam/width", 0.5)
    else:
        stairs.material.set("shader_param/width", 0.0)

                

I think that will give you the effect you desire. Ideally you would have

class_name Player 

inside your Player.gd script then the code becomes very simple:

func physics_process(delta):
    if Player in get_overlapping_bodies():
        stairs.material.set("shaderparam/width", 0.5)
    else:
        stairs.material.set("shader_param/width", 0.0)