Hello!
I’m new to these forums and also new to game development in general. So please forgive me if my issue is silly!
Regarding the issue, I’m having trouble having my player collect coins. It seems like the player is ignoring collisions (even though the player and coins both have collisions).
I also organized all the coins found in the level into a 2D node (see below). Perhaps that is causing issues? Although removing the node seems to do nothing.
Here is a gif of my problem below. The player phases through the coins:
If anyone is willing to help a beginner out, I’d be very appreciative!
Thank you!
first of all, you need to decide who should handle the collision.
In the case of your first screenshot, it’s coin_01.gd.
Therefore the body that has entered the coin must be the player, so please change line6 of coin_01.gd to check for the player instead.
feel free to print out the body.name above the if statement in order to see if the coin is detecting anything at all.
I will assume that the coin is on a “coin” collision layer and the player is masking the same “coin” collision layer.
I will also assume that the body_entered() signal is connected correctly for the coin.
btw, even if checking for the coin name was correct, the code still would not work because duplicates of all coin objects would be numbered.
Therefore I would refactor the if statement into: if body.name.begins_with("coin"):
more on that can be found in the documentation.
So as aforementioned, I’m really, REALLY, new to Gadot. I had no clue what signals even were when FreakyGoose mentioned them, so I spent a lot of time just learning about signals over the web and finding out what they do. But FreakyGoose was correct and I fixed the issue by simply connecting the signal.
This script also applies to all the coin duplicates found throughout the level, but I will keep Locher’s response in mind—especially the “if body.name.begins_with(“coin”):” statement in case I run into any issues regarding numbered duplicates.
Also, I removed any mention of the coin and player within the script. Not sure if it is best practice to incorporate a check for the player?
Thank you for all the help and support! I very much appreciate it!!!
It depends if anything other than the player can run into the coins. If something else can that doesn’t have a score variable then it will cause a problem.
It’s always nice to be thorough even though it means more thought and effort. You never know if you want to add objects like a box that your player can push around. In that case, the box touching the coin can make the coin disappear as well.