2
\$\begingroup\$

I am developing a simple platformer, I have three types of tiles right now:

  • Floor: does nothing on collision.
  • Bad: makes the player lose the game.
  • Good: makes the player beat the game.

I am trying to detect the collision with this code I found:

extends RigidBody2D

func _process(delta):
    var tile = get_parent().get_node("TileMap").get_cellv(get_parent().get_node("TileMap").world_to_map(position))
    print(tile)

The tile variable is supposed to contain the identifier of the type of tile the player is touching, but it doesn't, it always returns -1 (No exception or error, just -1).

I believe returning -1 means it isn't touching any tile, but it is.

This is using Godot 3.0 stable.

What am I missing?

Thank you for your patience.

\$\endgroup\$
0
\$\begingroup\$

I've found a way to make it work, I am not sure if this is the best possible solution but it works:

extends RigidBody2D

func _integrate_forces(state):
    var tilemap= get_parent().get_node("TileMap");  
    for i in range(get_colliding_bodies().size()):
        var tile_pos = tilemap.world_to_map(state.get_contact_local_position(i))
        var tile = tilemap.get_cellv(tile_pos)

This function is inside my player script. var tile will contain the type id of each colliding tile.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.