0 votes

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 :)

in Engine by (73 points)

1 Answer

+1 vote
Best answer

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.

by (3,144 points)
selected by

I understood but may you give a code example please?

Thanks in advance

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.

This made me understood everything

Thanks :-)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.