0 votes

I have a tile in my set named 'ice'. Is it possible to test collision between that particular tile and the player? If so, how?

in Engine by (197 points)

Can you be more specific? Is it topdown game - so Player is on "ice" tile or platformer - pleyer is on top/above a "ice" tile? Is Player KinematicBody2D?

Platformer using KinematicBody2D. Apologies.

1 Answer

0 votes

I think that you can't use RayCast2D to get specific tile, you will get specific object but I don't think that you can determine what tile index is that TileMapxxxxx object (someone correct me if i'm wrong). The only way I can think of is to check if Player is on the floor with is_on_floor() function. And you will need do add Position2D as a child of Player - one or two pixels below Player feet. Then you need to check if Position2D is inside tile of a "ice" name. Convert Position2D position from pixels to TileMap space: Pos2DTileSpace = Vector2( int (Position2D.x/CellSizeOfTileMap), int( Position2D.y/CellSizeOfTileMap) ), or use TileMap function world_to_map. Then check if Pos2DTileSpace have same index as your "ice" tile with get_cellv function of TileMap (you should preload TileMap in Player script with onready var myMap = get_node("MyTileMap") ). If Player is on the floor and tile index is equal "ice" tile then Player is on ice.

by (243 points)
edited by

is_on_floor is a Godot 3 function. I'm on 2.1.5.

Use RayCast2D from Player feet down some pixels instead of is on the floor, or better check for collision normal - if collision normal is pointig up you are on the floor. Then check with Position2D as I suggest it (or just use some Vector2 variable in Player script and set its value below Players feet 2-4 pixels and covert it in tilespace and check for index of tile that position is in).

I got it to work just using the world_to_map function. No raycast needed.

var tpos = tiles.world_to_map(plyr_obj.get_global_pos())
var itile = tiles.get_cellv(Vector2(tpos.x, tpos.y + 1))
print(itile)

This returns the tile ID. I can use this to toggle the ice flag.

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.