Godot Version
4.2.2
Question
`Hi. So, I’m working on a turn-based game where characters can only move a certain number of spaces each turn. The spaces could be visualized as squares, like the ones on a chess board. Whenever a square within a character’s movement limit is clicked on by the player for the character to move onto, the squares leading from the one that the character is standing on to his or her destination get added to an array, which I call “path.” I then try to use only certain squares in the path as waypoints for the character to move to. The idea I have is to perform a raycast from the starting square to the next one in the path array, and then to the next one, and the next one, and so on, until the ray hits an obstacle. Once it does, the last square that the raycast touched before hitting an obstacle gets added to an array that I call “waypoints,” and this process is repeated until the raycast touches the character’s destination square, except, once a waypoint is added to the array, the ray gets cast from that waypoint, instead of from the starting square. Then, the character moves to each waypoint one by one. But, I seem to be having trouble typing the code for this. The character just keeps moving one square, and then stopping, no matter how many there are in the path. Here is what I have typed:
func RaycastToCell():
ray.enabled = true
var checking = 0
var waypointsDiscovered = 0
if checking < len(path):
if len(waypoints) > 0:
ray.global_position = waypoints[waypointsDiscovered - 1]["location"]
ray.target_position = path[checking]["location"]
else:
ray.global_position = startCell["location"]
ray.target_position = path[checking]["location"]
if ray.is_colliding():
if ray.get_collider().is_in_group("Obstacles"):
waypoints.push_back(path[checking - 1]["location"])
waypointsDiscovered += 1
else:
checking += 1
print(checking)
if checking == len(path):
waypoints.push_back(destination["location"])