Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question A* Pathfinding Project: How to get remaining distance on the path

Discussion in 'Scripting' started by Yoshinori_Date, Oct 18, 2020.

  1. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    I want to make a very simple script that gonna do only that, show how far it is to the target using A* pathfinding, i am a complete noob at coding so explanation would be appreciated.
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    This can give you good start.
    Code (csharp):
    1.  
    2.  
    3. Seeker.StartPath(start, target, OnPathCalculated, graphMask);
    4.  
    5.    void OnPathCalculated(Path path){
    6.  
    7.        if(path.error){
    8.            return;
    9.        }
    10.  
    11.         float pathLength = path.GetTotalLength()
    12. }
    13.  
    Edit:I have assumed you are using A* Pathfinding by aron granberg.
     
  3. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    yes, i using that asset, but the free version of it
     
  4. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Pathfinding;
    5.  
    6. public class PFing : MonoBehaviour
    7. {
    8.  
    9.     Seeker.StartPath(start, target, OnPathCalculated, graphMask);
    10.    void OnPathCalculated(Path path)
    11.     {
    12.  
    13.         if (path.error)
    14.         {
    15.             return;
    16.         }
    17.  
    18.         float pathLength = path.GetTotalLength()
    19. }
    20.  
    21. }
    for every field in this "Seeker.StartPath(start, target, OnPathCalculated, graphMask);" line it tells me that "it does not exist in the current context" what am i missing?
     
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    Ok i see you are real beginner.
    Try this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Pathfinding;
    5.  
    6. public class PFing : MonoBehaviour
    7. {
    8.  
    9. public Seeker seeker;
    10.  
    11.  
    12. void Start(){
    13.     seeker.StartPath(start, target, OnPathCalculated);
    14. }
    15.  
    16.    void OnPathCalculated(Path path)
    17.     {
    18.  
    19.         if (path.error)
    20.         {
    21.             return;
    22.         }
    23.  
    24.         float pathLength = path.GetTotalLength()
    25. }
    26.  
    27. }
     
  6. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    huh, is it supposed to return length in console instead of public float or am i missing something?
     
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    May I ask what are you need it for ?
    Because it looks like your programming skill is "Hello World" level.
    And it might be good idea to start with a basics.
     
  8. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    i make an xcom like game and want to get the path length to see if it's more or less then a treshold to spawn a tile that player can click on, and i use playmaker so most of the logic i made in it, but there is no action for astar to get the length of the path which is the crucial for field of movement visualization, i managed to make it with unity navmesh but it's too slow, so i need astar
     
  9. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    and yes, my coding skills are low, i do understand principles but my problem is syntax and what even can i do, and i have no clue where to look for things i need, i look at astar documentation but it's just confusing for me
     
  10. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    I will not help you with playmaker and how to grab data from component to playmaker since I don't use it.
    But since it is a turn based game and you are just a beginner I suggest using:
    Code (csharp):
    1.  
    2.         var path = Pathfinding.ABPath.Construct(start, end);
    3.         AstarPath.BlockUntilCalculated(path);
    4.  
    This will block thread until path is completed.
     
  11. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    where can i found an extensive explanation on astar functions? i tried to google what "path.GetTotalLength" actually do, but all the documentations i found doesn't really tell much
     
  12. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
  13. Yoshinori_Date

    Yoshinori_Date

    Joined:
    Dec 6, 2018
    Posts:
    18
    oh, there were a search bar... didn't saw it for all this time for some reason