Skip to main content
3 of 3
added 432 characters in body
gunza
  • 13
  • 5

I can't add the change I made while the game is running to the list?

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();
}
*/
}
gunza
  • 13
  • 5