Skip to main content
added 432 characters in body
Source Link
gunza
  • 13
  • 5

Node script is shared belowI realized that my problem has nothing to do with tilemap or something so I need to update my question. The NodeCalculateAgain()

In the script works fine whenbelow, I run the game for CalculateDirections() function in the first time. However, whileFixedUpdate() function to check and update the game is running, wheninter-node route. So isn't there is a changeway to have this run only when needed?

As you can see in the mapscript, I gave the list specifiedfunction named CalculateAgain()to a button, for example, and tried it while in the script needsgame, but it didn't work. It didn't update the path that needed to be updated (it should add the necessary elements to the list because the directions of the node whose list is empty are available) but it does not add.

Node gameobject is created It only works with Update or FixedUpdate. But this creates a performance problem and Node script is assigned to this objectnot necessary anyway. Later,I only want it was assigned to "Rule Tile" withwork when the addcross-on of the additional package called "Tilemap Extras". Then, these points were drawn on the tilemap named "Nodes" with the tile palette.

I have made explanations on the sample pictures so that my question does not become complicated and there are no missing partsnode route needs to be updated.

First image:

Unity Editor Screenshot

Second image:

Unity Editor Screenshot

using UnityEngine;
using System.Collections.Generic;

public class Node : MonoBehaviour
{
public List<Vector2> availableDirections = new List<Vector2>();

public LayerMask obstacleLayer;

private void Start()
{
    CalculateDirections();
}

publicprivate void CalculateDirectionsFixedUpdate()
{
    CalculateDirections();
}

public void CalculateDirections()
{

    CheckAvailableDirections(Vector2.up);
    CheckAvailableDirections(Vector2.down);
    CheckAvailableDirections(Vector2.left);
    CheckAvailableDirections(Vector2.right);

     Debug.Log("CalculateDirections() FUNC WORKED");
}

privatepublic void CheckAvailableDirections(Vector2 direction)
{
    RaycastHit2D hit = Physics2D.BoxCastRaycast(this.transform.position, Vector2direction, 3.one0f, *obstacleLayer); 1

    if (hit.5f,collider 0== null)
    {
        Debug.0fDrawRay(this.transform.position, direction, 6* 3.0f, thisColor.obstacleLayerblue, 30.0f);
 
    Debug.Log("BOXCAST HIT WORKED"  this.availableDirections.Add(direction);
 
    if}
 (hit.collider == null) else
    {
        Debug.DrawRay(this.availableDirectionstransform.Add(position, direction * 3.0f, Color.yellow, 30.0f);
 
        Debug.Log("ADDED"COLLIDER LIST"ÇALIŞMADI");
    }
}

 /*
public void CalculateAgain()
{
    CalculateDirections();
}
*/
}

Node script is shared below. The Node script works fine when I run the game for the first time. However, while the game is running, when there is a change in the map, the list specified in the script needs to be updated (it should add the necessary elements to the list because the directions of the node whose list is empty are available) but it does not add.

Node gameobject is created and Node script is assigned to this object. Later, it was assigned to "Rule Tile" with the add-on of the additional package called "Tilemap Extras". Then, these points were drawn on the tilemap named "Nodes" with the tile palette.

I have made explanations on the sample pictures so that my question does not become complicated and there are no missing parts.

First image:

Unity Editor Screenshot

Second image:

Unity Editor Screenshot

using UnityEngine;
using System.Collections.Generic;

public class Node : MonoBehaviour
{
public List<Vector2> availableDirections = new List<Vector2>();

public LayerMask obstacleLayer;

private void Start()
{
    CalculateDirections();
}

public void CalculateDirections()
{
    
    CheckAvailableDirections(Vector2.up);
    CheckAvailableDirections(Vector2.down);
    CheckAvailableDirections(Vector2.left);
    CheckAvailableDirections(Vector2.right);

     Debug.Log("CalculateDirections() FUNC WORKED");
}

private void CheckAvailableDirections(Vector2 direction)
{
    RaycastHit2D hit = Physics2D.BoxCast(this.transform.position, Vector2.one * 1.5f, 0.0f, direction, 6.0f, this.obstacleLayer);
 
    Debug.Log("BOXCAST HIT WORKED");
 
    if (hit.collider == null)
    {
        this.availableDirections.Add(direction);
 
        Debug.Log("ADDED LIST");
    }
}

public void CalculateAgain()
{
    CalculateDirections();
}
}

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();
}
*/
}
edited tags, embedded images
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

Node script is shared below. The Node script works fine when I run the game for the first time. However, while the game is running, when there is a change in the map, the list specified in the script needs to be updated (it should add the necessary elements to the list because the directions of the node whose list is empty are available) but it does not add.

Node gameobject is created and Node script is assigned to this object. Later, it was assigned to "Rule Tile" with the add-on of the additional package called "Tilemap Extras". Then, these points were drawn on the tilemap named "Nodes" with the tile palette.

I have made explanations on the sample pictures so that my question does not become complicated and there are no missing parts.

First image: https://imgur.com/FsV20VG

Unity Editor Screenshot

Second image: https://imgur.com/XDUmUQG

Unity Editor Screenshot

using UnityEngine;
using System.Collections.Generic;

public class Node : MonoBehaviour
{
public List<Vector2> availableDirections = new List<Vector2>();

public LayerMask obstacleLayer;

private void Start()
{
    CalculateDirections();
}

public void CalculateDirections()
{
    
    CheckAvailableDirections(Vector2.up);
    CheckAvailableDirections(Vector2.down);
    CheckAvailableDirections(Vector2.left);
    CheckAvailableDirections(Vector2.right);

    Debug.Log("CalculateDirections() FUNC WORKED");
}

private void CheckAvailableDirections(Vector2 direction)
{
    RaycastHit2D hit = Physics2D.BoxCast(this.transform.position, Vector2.one * 1.5f, 0.0f, direction, 6.0f, this.obstacleLayer);

    Debug.Log("BOXCAST HIT WORKED");

    if (hit.collider == null)
    {
        this.availableDirections.Add(direction);

        Debug.Log("ADDED LIST");
    }
}

public void CalculateAgain()
{
    CalculateDirections();
}
}

Node script is shared below. The Node script works fine when I run the game for the first time. However, while the game is running, when there is a change in the map, the list specified in the script needs to be updated (it should add the necessary elements to the list because the directions of the node whose list is empty are available) but it does not add.

Node gameobject is created and Node script is assigned to this object. Later, it was assigned to "Rule Tile" with the add-on of the additional package called "Tilemap Extras". Then, these points were drawn on the tilemap named "Nodes" with the tile palette.

I have made explanations on the sample pictures so that my question does not become complicated and there are no missing parts.

First image: https://imgur.com/FsV20VG

Second image: https://imgur.com/XDUmUQG

using UnityEngine;
using System.Collections.Generic;

public class Node : MonoBehaviour
{
public List<Vector2> availableDirections = new List<Vector2>();

public LayerMask obstacleLayer;

private void Start()
{
    CalculateDirections();
}

public void CalculateDirections()
{
    
    CheckAvailableDirections(Vector2.up);
    CheckAvailableDirections(Vector2.down);
    CheckAvailableDirections(Vector2.left);
    CheckAvailableDirections(Vector2.right);

    Debug.Log("CalculateDirections() FUNC WORKED");
}

private void CheckAvailableDirections(Vector2 direction)
{
    RaycastHit2D hit = Physics2D.BoxCast(this.transform.position, Vector2.one * 1.5f, 0.0f, direction, 6.0f, this.obstacleLayer);

    Debug.Log("BOXCAST HIT WORKED");

    if (hit.collider == null)
    {
        this.availableDirections.Add(direction);

        Debug.Log("ADDED LIST");
    }
}

public void CalculateAgain()
{
    CalculateDirections();
}
}

Node script is shared below. The Node script works fine when I run the game for the first time. However, while the game is running, when there is a change in the map, the list specified in the script needs to be updated (it should add the necessary elements to the list because the directions of the node whose list is empty are available) but it does not add.

Node gameobject is created and Node script is assigned to this object. Later, it was assigned to "Rule Tile" with the add-on of the additional package called "Tilemap Extras". Then, these points were drawn on the tilemap named "Nodes" with the tile palette.

I have made explanations on the sample pictures so that my question does not become complicated and there are no missing parts.

First image:

Unity Editor Screenshot

Second image:

Unity Editor Screenshot

using UnityEngine;
using System.Collections.Generic;

public class Node : MonoBehaviour
{
public List<Vector2> availableDirections = new List<Vector2>();

public LayerMask obstacleLayer;

private void Start()
{
    CalculateDirections();
}

public void CalculateDirections()
{
    
    CheckAvailableDirections(Vector2.up);
    CheckAvailableDirections(Vector2.down);
    CheckAvailableDirections(Vector2.left);
    CheckAvailableDirections(Vector2.right);

    Debug.Log("CalculateDirections() FUNC WORKED");
}

private void CheckAvailableDirections(Vector2 direction)
{
    RaycastHit2D hit = Physics2D.BoxCast(this.transform.position, Vector2.one * 1.5f, 0.0f, direction, 6.0f, this.obstacleLayer);

    Debug.Log("BOXCAST HIT WORKED");

    if (hit.collider == null)
    {
        this.availableDirections.Add(direction);

        Debug.Log("ADDED LIST");
    }
}

public void CalculateAgain()
{
    CalculateDirections();
}
}
Source Link
gunza
  • 13
  • 5

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

Node script is shared below. The Node script works fine when I run the game for the first time. However, while the game is running, when there is a change in the map, the list specified in the script needs to be updated (it should add the necessary elements to the list because the directions of the node whose list is empty are available) but it does not add.

Node gameobject is created and Node script is assigned to this object. Later, it was assigned to "Rule Tile" with the add-on of the additional package called "Tilemap Extras". Then, these points were drawn on the tilemap named "Nodes" with the tile palette.

I have made explanations on the sample pictures so that my question does not become complicated and there are no missing parts.

First image: https://imgur.com/FsV20VG

Second image: https://imgur.com/XDUmUQG

using UnityEngine;
using System.Collections.Generic;

public class Node : MonoBehaviour
{
public List<Vector2> availableDirections = new List<Vector2>();

public LayerMask obstacleLayer;

private void Start()
{
    CalculateDirections();
}

public void CalculateDirections()
{
    
    CheckAvailableDirections(Vector2.up);
    CheckAvailableDirections(Vector2.down);
    CheckAvailableDirections(Vector2.left);
    CheckAvailableDirections(Vector2.right);

    Debug.Log("CalculateDirections() FUNC WORKED");
}

private void CheckAvailableDirections(Vector2 direction)
{
    RaycastHit2D hit = Physics2D.BoxCast(this.transform.position, Vector2.one * 1.5f, 0.0f, direction, 6.0f, this.obstacleLayer);

    Debug.Log("BOXCAST HIT WORKED");

    if (hit.collider == null)
    {
        this.availableDirections.Add(direction);

        Debug.Log("ADDED LIST");
    }
}

public void CalculateAgain()
{
    CalculateDirections();
}
}