Is there a way to add lighting such that I can detect if my character in getting hit by light?

lonskynelonskyne Posts: 7Member

I'm making a platformer/side scroller pixel art game. I need a way to be able to see if my player is being hit by a light source so I can see if he is casting a shadow. Is there a way to do this and how would I even begin with that in Godot? Thanks!


Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 3,103Admin

    There is a couple ways I can see for doing this, though I have not tried either myself.

    The first, easiest, and most likely to work way would be to place Area2D nodes on every light source you have in your game, and then check if the player is within one of these areas. It would not be detecting if the player is in a light source necessarily, but if done correctly it would give the illusion of knowing when the player is in the light or not. I know this should be doable, and with some additional post processing (like using raycasts to detect if the light can actually reach the player) you can use it for line-of-sight for AI entities as well.

    The second way is to write a shader and use the Light function. You could expose a boolean uniform in the shader and set the boolean to true in the Light function when the sprite it exposed to light and false when it is not. You could then read the results of the boolean in GDScript and have your code react accordingly. I do not know if this method would actually work, but it might be a way to get a more accurate result of whether the player is in the light or not.

    Hopefully this helps, and welcome to the forums! :smile:

    (Side note: Please do not create duplicate posts. Thanks!)

  • lonskynelonskyne Posts: 7Member
    edited January 2

    @TwistedTwigleg Thanks for the suggestions, will try them out. Sorry for making a duplicate, I got the comment that it got caught in the queue because my email wasn't verified so I verified and then made the post again thinking the first one got removed.

  • TwistedTwiglegTwistedTwigleg Posts: 3,103Admin

    @lonskyne said:
    @TwistedTwigleg Thanks for the suggestions, will try them out. Sorry for making a duplicate, I got the comment that it got caught in the queue because my email wasn't verified so I verified and then made the post again thinking the first one got removed.

    Hopefully the suggestions will work. I have a loose idea of how they might work in an actual project, but I have not tested or used them myself just yet.

    As far as the duplicate post: No worries, it is all good. :smile:

  • SIsilicon28SIsilicon28 Posts: 757Moderator

    @TwistedTwigleg said:
    ...
    The second way is to write a shader and use the Light function. You could expose a boolean uniform in the shader and set the boolean to true in the Light function when the sprite it exposed to light and false when it is not. You could then read the results of the boolean in GDScript and have your code react accordingly. I do not know if this method would actually work, but it might be a way to get a more accurate result of whether the player is in the light or not.
    ...

    For your second approach, I'm not sure how that would work since uniform variables can't be set by the shader itself. :/

  • TwistedTwiglegTwistedTwigleg Posts: 3,103Admin

    Interesting, I did not know that uniforms cannot be set by the shader (documentation). Unfortunately, that limitation does indeed make the second approach impossible. :sweat:

    A shame, and makes me wonder about the use cases for get_shader_param, since the uniform can only change through script.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file