0 votes

Hello, I created an AStar pathfinding with the video from GDQuest, then I did when you click on a character and then an object, in this case a tree. Then the character runs to the tree. But the tree is not recognized as an obstacle. I used in the script:

extends Node2D

onready var your_tile_map = get_parent (). get_node ("TileMap") # The path for your actual TileMap

func _ready ():
var grid_position = your_tile_map.world_to_map (self.global_position) # Converting position
your_tile_map.set_cellv (grid_position, 0) # Setting a collision tile

and in the script it creates a tile in the correct position of my tree, but the tile is not recognized as an obstacle for whatever reason. The ID is perfect, ID 0 stands for the object where there is an obstacle. So if I put the obstacle on the tree myself by hand then it works without problems but if I do not do that in the script.

Does anyone know what I mean and has a solution? if so it would be super nice!

Godot version 3.2.3
in Engine by (85 points)

1 Answer

+1 vote
Best answer

I use a second tilemap to map collisions. Astar works with points and connections between those points, blocks that have an object cannot have points ... To know this, just create an array containing the ids of the free blocks (it may be the opposite but I believe that you will have more blocks occupied than free) and pass a "for x in range" over the main tilemap identifying the id of each tile and setting it as free or busy in the second tilemap ... Then just run the creation of Astar points on this secondary tilemap ignoring or Tiles blocked.

by (211 points)
selected by
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.