I realized that my problem has nothing to do with tilemap or something so I need to update my question.CalculateAgain()
In the script below, I run the CalculateDirections()
function in the FixedUpdate()
function to check and update the inter-node route. So isn't there a way to have this run only when needed?
As you can see in the script, I gave the function named CalculateAgain()
to a button, for example, and tried it while in the game, but it didn't work. It didn't update the path that needed to be updated. It only works with Update or FixedUpdate. But this creates a performance problem and is not necessary anyway. I only want it to work when the cross-node route needs to be updated.
using UnityEngine;
using System.Collections.Generic;
public class Node : MonoBehaviour
{
public List<Vector2> availableDirections = new List<Vector2>();
public LayerMask obstacleLayer;
private void Start()
{
CalculateDirections();
}
private void FixedUpdate()
{
CalculateDirections();
}
public void CalculateDirections()
{
CheckAvailableDirections(Vector2.up);
CheckAvailableDirections(Vector2.down);
CheckAvailableDirections(Vector2.left);
CheckAvailableDirections(Vector2.right);
}
public void CheckAvailableDirections(Vector2 direction)
{
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, direction, 3.0f, obstacleLayer);
if (hit.collider == null)
{
Debug.DrawRay(this.transform.position, direction * 3.0f, Color.blue, 30.0f);
this.availableDirections.Add(direction);
}
else
{
Debug.DrawRay(this.transform.position, direction * 3.0f, Color.yellow, 30.0f);
Debug.Log("COLLIDER ÇALIŞMADI");
}
}
/*
public void CalculateAgain()
{
CalculateDirections();
}
*/
}
SetTile(cell, null)
. The collider over there is also getting up. Then I call CalculateAgain() and hope it will add directions of the middle point to the list. So theoretically, there's no collider for ray to hit, right? \$\endgroup\$SetTile(cell, null)
the red dot there still thinks there is a wall and the "ray" does not enter the if block. This happens while the game is running. \$\endgroup\$