• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tomaskillian41 · Feb 02 at 05:33 PM · scripting problempathfinding

Duda script A Start Pathfinder

Hi, I'm trying to implement A* Start in unity but I don't understand why the same path in reverse fills the closed list with more nodes, example I pass the starting position Vector 2(2,2) and the target Vector(10 ,12) the closed list ends with 200 nodes but if I do the same thing but in reverse closed it ends with 448 nodes.

 class NODO : IComparable
 {
     public Posicion Pos;
     public float F,G;
     public bool Ocupado = false;
     public NODO Next;
     public NODO(Posicion _Pos)
     {
         Pos = _Pos;
     }
     public int CompareTo(object obj)
     {
         if (obj == null) return -1;
         NODO N = (NODO)obj;
         if (N != null)
         {
             return this.F.CompareTo(N.F);
         }
         else
         {
             throw new NotImplementedException();
         }
     }


 public class PathfinderA : MonoBehaviour
 {
     NODO[,] Grid;
     int Ancho, Alto;
     public void IniciarGridNodos(int _Ancho, int _Alto)
     {
         Ancho = _Ancho;
         Alto = _Alto;
         Grid = new NODO[Ancho, Alto];
         for (int x = 0; x < Ancho; x++)
         {
             for (int y = 0; y < Alto; y++)
             {
                 Grid[x, y] = new NODO(new Posicion(x, y));
             }
         }
     }
     public void Pathfinder(Vector2 Inicio, Vector2 Target)
     {
         NODO inicio = Grid[(int)Inicio.x, (int)Inicio.y];
         NODO target = Grid[(int)Target.x, (int)Target.y];
         List<NODO> Abierta = new List<NODO>();
         List<NODO> Cerrada = new List<NODO>();
         Abierta.Add(inicio);
         while (Abierta.Count > 0)
         {
             Abierta.Sort();
             NODO Actual = Abierta[0];
             if (Actual == target)
             {
                 //Camino Encontrado
                 Debug.Log("Camino Encontrado");
                 break;
             }
             //Buscar Adyasentes y Calcular Costos
             List<NODO> Adyasentes = GetAdyasentes(Actual);
             for (int i = 0; i < Adyasentes.Count; i++)
             {
                 if (!Cerrada.Contains(Adyasentes[i]) && !Adyasentes[i].Ocupado)
                 {
                     float newCostG = Actual.G + 10;
                     if (newCostG < Adyasentes[i].G || !Abierta.Contains(Adyasentes[i]))
                     {
                         Adyasentes[i].G = newCostG;
                         Adyasentes[i].F = newCostG + Vector2.Distance(new Vector2(Adyasentes[i].Pos.x, Adyasentes[i].Pos.y), Target);
                         Adyasentes[i].Next = Actual;
                         if (!Abierta.Contains(Adyasentes[i]))
                         {
                             Abierta.Add(Adyasentes[i]);
                         }
                     }
                 }
             }
             Cerrada.Add(Actual);
             Abierta.RemoveAt(0);
         }
         Debug.Log(Cerrada.Count);
     }
     List<NODO> GetAdyasentes(NODO Actual)
     {
         Posicion Pos = Actual.Pos;
         List<NODO> Adyasentes = new List<NODO>();
         //Izquirda
         if (Pos.x - 1 > 0)
         {
             Adyasentes.Add(Grid[Pos.x - 1, Pos.y]);
         }
         //Abajo
         if (Pos.y - 1 > 0)
         {
             Adyasentes.Add(Grid[Pos.x, Pos.y - 1]);
         }
         //Derecha
         if (Pos.x + 1 < Ancho - 1)
         {
             Adyasentes.Add(Grid[Pos.x + 1, Pos.y]);
         }
         //Arriba
         if (Pos.y + 1 < Alto - 1)
         {
             Adyasentes.Add(Grid[Pos.x, Pos.y + 1]);
         }
         return Adyasentes;
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

239 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to visualize each step of Pathfinding Algorithm on Screen? 1 Answer

Problem with calculated path of a NavMeshAgent 0 Answers

Destroying an instatiated game object with a script attached creates problems for the rest of instatiated game objects with the same script 0 Answers

A* Pathfinding on click? 0 Answers

Is there a way for me to find the walkable edges of my traversable game map? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges