0
\$\begingroup\$

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();
}
*/
}
\$\endgroup\$
5
  • \$\begingroup\$ You could check what is actually hit by your raycast and thus stops it. And only on your first run is available Directions empty, might needs a clear as well when you call CalculateAgain \$\endgroup\$
    – Zibelas
    Commented Jan 15, 2022 at 20:53
  • \$\begingroup\$ I destroy the wall (tile) at the problem point with 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\$
    – gunza
    Commented Jan 15, 2022 at 22:13
  • \$\begingroup\$ So even though I destroyed the wall with 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\$
    – gunza
    Commented Jan 15, 2022 at 22:38
  • \$\begingroup\$ If the destruction of the tile is making the problem, you should include the code for that as well \$\endgroup\$
    – Zibelas
    Commented Jan 15, 2022 at 23:13
  • \$\begingroup\$ After the tile is destroyed (the pink wall in that square), there is no collider left for the "ray" to hit. That's why it should work. So there is no problem in the disappearance of the tile. The problem is that the collider is perceived as if it is not present. \$\endgroup\$
    – gunza
    Commented Jan 16, 2022 at 0:30

0

You must log in to answer this question.

Browse other questions tagged .