+1 vote

Good day,

i have a problem with the detection of collisions in a 2D top down project.

The player is a KinematicBody2d with the movement via moveandcollide. In the map the player is on his own PB2D layer and mask. The tiles in the tilemap have all some CP2D and the tilemap is on its own layer and mask.

The collision detection works if both player and tilemap are on the same layer but then the player cant "go through / go over" the tile.

I want is to detect if the player is moving over the tile with the name or number x and then cut his move speed in half.

Is there away to make this possible or do i have to rethink anything?

The code can be found https://github.com/Fedena22/NGP

Thanks for your help.

in Engine by (16 points)

You can get Player position in Tile space with function in TIleMap class: worldtomap, e.g.: var playerTilePosition = TileMap.worldtomap(Player.position).
Then you need to check if playerTilePosition has same index as a tile that should slow down Player, e.g.:
if TileMap.get_cellv(PlayerTilePosition) == "YourSlowDownTIleIndex":
playerVelocity = playerVelocity/2

Becouse you are using KinematicBody2D, on tiles that should slow down Player you should use Area2d, and then check with: body_entered().

1 Answer

+2 votes
Best answer

moveandcollide is going to make the KinematicBody2d stop as if the other thing is solid so you need a different method.

In your example you have an obstacle that changes behavior of the player, but does not stop it in its tracks. If I was doing this I would add an additional child (probably Area2D) that I would use as a sensor. I would add a new collision layer bit like "SlowDown" and I would mask the Area2D to detect it. I would then use a signal like body_entered to detect when the player is touching that object and halve the speed or set a flag or however you want the behavior to flow.

by (116 points)
selected by

Thanks helped me a lot

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 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 webmaster@godotengine.org with your username.

Categories