Using move-and-collide for TileMap collision detection

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By IOAN SAVOIU

I’m very new with Godot, and tried to get a collision between a moving element(KinematicBody2D) and Tilemap objects(like trees) where I have added a collision
Into my KinematicBody2D script I have added a collision detection

func _physics_process(delta): 
	var collision = move_and_collide(velocity * delta)
	if collision != null:
		print("collision")

I have already tried with move_and_slide
but Both aren’t working.
Do you have an idea on how to do it?

Could you give us more details about the collisions? Do the trees have just the collision shapes on them? Do they have an Area2D on them?

Ertain | 2020-01-23 18:23

Thanks for asking,
I have set the trees collision with a polygon by surrounding the shape of the tree into the tileset editor.
The trees are then added with an individually tileMap Node of my Tree level
More about the projectile(the ‘moving part’ I told), here’s how the variables and functions are created:

extends KinematicBody2D

export (int) var speed = 1000
var velocity = Vector2()

func _ready():
    pass 

#calling from player when player is shooting
func start(_position,_direction):
    position = _position
    rotation = _direction.angle()
    velocity = _direction * speed

func _physics_process(delta): 
    var collision = move_and_collide(velocity * delta)
    if collision != null:
        print("free bullet",collision)
        queue_free()

That’s all I can provide you that I think is the most important

IOAN SAVOIU | 2020-01-23 20:00

Do the trees only have a shape? IIRC, they need to first have have something like a StaticBody2D or an Area2D node attached for any meaningful collision.

Ertain | 2020-01-23 22:16