0 votes

I have a tileset with tiles that have sprite and Area2D as a child(with shape) .
And a player's tank that can move. He is a kinematic body with collision shape. The tiles are grass and road.
My question is how i can check if a player's tank is colliding with those tiles that have Area2D because i want to modify tank's max speed when he is on road

in Engine by (33 points)

2 Answers

0 votes

TileSet does not support using Area2D, only StaticBody2D for collisions.

You would have to either add the areas separately to your map, or have your tank check what tile type it's on using get_cell() when it moves.

by (18,211 points)

Ok, so i have another idea.

I have searched and find that i can get "shape" property from tileset if i know tile ID
My new idea is to create new scene with root as "Area2D" node and child as shape, let's call this scene "road"

and then at game start i will iterate through all tilemap cells and get tile IDs. With that i want check if current tile has a "shape" if so, then i want to instantiate "road" scene and set collision shape to the same as tile's shape

I write code for that:

extends TileMap

onready var road = preload("res://Terrain/RoadScript.tscn")

func _ready():
    print("Get Cell IDs")
    var tileset = tile_set
    for i in get_used_cells():
        var cellID = get_cellv(i)
        var shapeCount = tileset.tile_get_shape_count(cellID)
        print("shapeCount: "+str(shapeCount))
        if shapeCount > 0:
            var shape = tileset.tile_get_shape(cellID, 0)
            var roadScene = road.instance()
            add_child(roadScene)
            roadScene.get_node("CollisionShape2D").shape = shape

But my new problem is, how i can add a shape to the tile in tileset:
enter image description here
I tried to add collisionshape2d child to the tile sprite but my script shows shapeCount = 0 for every tile

0 votes

I have a player with Area2D and tilemap that has several tiles with collision set on them.
And I use signal -> onPlayerbodyentered for the Player.
And it works fine when the player hits the tile.

by (14 points)
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