How to get color of the floor using raycast in godot

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

I am making a platformer in godot engine. I added some dust particles appear when the character is moving. I set the default color of the particles as green as my character is mostly walking on grass blocks. But when he is walking on other blocks it still appears green and does not look very realistic.

So how can i get the color of object the raycast collides with

Thanks :slight_smile:

:bust_in_silhouette: Reply From: Ertain

Have theRayCast check for a collision (use the is_colliding() function) in the _physics_process() callback or some similar function. Then use the get_collider() function to check for whether the object that’s colliding is some block (you might have to put them in a group or on their own collision layer). Then check for whether the block has some color property. If necessary, give the block a color property so as to give the particles their color.

How that helps.

I understood but may you give a code example please?

Thanks in advance

TopBat69 | 2022-05-28 03:40

Try this on for size. One warning: do not just cut and paste this code, as it will probably not work the way you expect it to work (if it works at all). Please try to study it before using it.

# Assign your color here.
var color
func _physics_process(change):
    if some_raycast.is_colliding():
        var le_collider = some_raycast.get_collider()
        # Check if "le_collider" is of the same class as the grass (or dirt, or metal, or rock, or...) block. For this example, we'll assume it's a TileMap.
        if le_collider is TileMap:
            # Now check to see if the TileMap is in the "Ground" group (or whatever group you want to place these blocks in).
            if le_collider.is_in_group("Ground"):
                # Check for which block is being used. How do you do this? That's up to you. Here, I'll match names. Side note: isn't this starting to look like an "if" arrow?
                match le_collider.get_name():
                        color = "green"
                        color = "brown"
                        color = "silver"
                       # Couldn't figure out the color. Print an error and exit.
                       push_error("Couldn't determine block color.")
    # Now assign the color. How you do that is up to you. The following function is made up.

Ertain | 2022-05-28 06:35

This made me understood everything

Thanks :slight_smile:

TopBat69 | 2022-05-28 07:09