Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Nk.Andrei · Jun 03, 2013 at 06:27 PM · pathfindingalgorithma

Help with A* algorithm

I need some help with A* algorithm.I hit an obstacle and cant figure it out.Please help

 #pragma strict
 
 
 import System.Collections.Generic;
 
 var agent             : GameObject;
 
 var agentList             : array;
 
 var i                 : int;
 
 var searchTrue             : boolean = false;
 
 var lowestValueList         : float[];
 
 var lowestValue         : float = Mathf.Infinity;
 
 var costF             : float;
 
 var neighbourValue         : cost;
 
 var children             : Transform;
 
 var x                 : int;
 
 
 
 //list ----the closed list
 //pathList ----the open lis
 
 
 function Start () {
 }
 
 function Update () 
 {
 }
 
 function OnTriggerStay (collider : Collider)
 {
     var child         : Transform;    
     
     if(searchTrue == true) ///if it is true then search for children
     {
         if(collider.transform.tag == "cube" && collider.transform.parent == null && collider.transform.GetComponent(cost).availible == true && collider.transform.GetComponent(cost).inList == false) ///search for children ....if they have no parent then this object becomes theyre parent
         {
             collider.transform.parent = gameObject.transform;
         }
     }
     
     for( child in gameObject.transform)
     
     {
     
         var agent     : GameObject     = GameObject.Find("Sphere");
         var agentList     : array         = agent.transform.GetComponent("array");
         
         if(child.transform.localPosition == Vector3(1,0,0) || child.transform.localPosition == Vector3(-1,0,0) || child.transform.localPosition == Vector3(0,0,1) || child.transform.localPosition == Vector3(0,0,-1))
         {
             child.transform.GetComponent(cost).gCost =child.transform.parent.GetComponent(cost).gCost + 10;
         }
         if(child.transform.localPosition == Vector3(1,0,1) || child.transform.localPosition == Vector3(-1,0,-1) || child.transform.localPosition == Vector3(1,0,-1) || child.transform.localPosition == Vector3(-1,0,1))
         {
             child.transform.GetComponent(cost).gCost = child.transform.parent.GetComponent(cost).gCost + 14;
         }
         //////add the g cost to the children
         
         
         if(!agentList.pathList.Contains(child) ) ////add it to the open list
         {
             agentList.pathList.Add(child);      
         }    
         
         
         for(i = 0 ; i < agentList.pathList.Count; i++) 
         {
             neighbourValue = agentList.pathList[i].transform.GetComponent(cost);
         
             costF              = neighbourValue.fCost;
             lowestValueList     = [costF];
             
             for(x = 0 ; x < lowestValueList.length ; x++)
             {
                 if(lowestValueList[x] < lowestValue )
                 {
                         lowestValue = lowestValueList[x];
                 }
             }/////calculate the lowest f cost
                 if(agentList.pathList[i].transform.parent == gameObject.transform.parent && agentList.pathList[i].transform.GetComponent(cost).fCost == lowestValue && agentList.pathList.Contains(agentList.pathList[i]) )
                 {
                     
                     if((agentList.pathList[i].transform.GetComponent(cost).gCost + agentList.pathList[i].transform.parent.GetComponent(cost).gCost) < (gameObject.transform.GetComponent(cost).gCost+agentList.pathList[i].transform.GetComponent(cost).gCost))   ///if the child and the current gameObject have the same parent and the g cost is lower
                     {
                         agentList.list.Add(agentList.pathList[i]);
                         
                                                 agentList.pathList.Remove(agentList.pathList[i]);                        
                         gameObject.transform.GetComponent(cost).inList = true;
                         gameObject.transform.GetComponent(parentChild).searchTrue = false;
                     }    
                 }
                 else if(agentList.pathList[i].transform.parent == gameObject.transform  &&  !agentList.list.Contains(agentList.pathList[i]) && agentList.pathList.Contains(agentList.pathList[i]) &&  agentList.pathList[i].transform.GetComponent(cost).fCost == lowestValue )
                 {
                     
                     
                         agentList.list.Add(agentList.pathList[i]);  
 agentList.pathList.Remove(agentList.pathList[i]);                    gameObject.transform.GetComponent(parentChild).searchTrue = false;
 
             }
         }
     }
 
 }

The code isnt giving me any errors......it gives me a path but its not the best one and it dosent work every time.....most of the times it cant find a path . the code above is from the cells i used to make a grid.

Any help is greatly appreciated.

Comment
Add comment · Show 4
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
avatar image Benproductions1 · Jun 04, 2013 at 12:07 AM 2
Share

Have you tried debugging the problem, or are you just like: Oh, it doesn't work... lets ask others?

avatar image Nk.Andrei · Jun 04, 2013 at 04:08 PM 0
Share

I dont need any code example....just let me know what steps I am missing or what steps have I done wrong and were.I just cant seem to find out what I am doing wrong.

avatar image Graham-Dunnett ♦♦ · Jun 04, 2013 at 04:10 PM 0
Share

We don't know what steps you are missing. What steps have you tried to resolve this?

avatar image SeventeenthShard · Aug 10, 2013 at 07:55 PM 1
Share

I know it's not the answer you want, but you'll likely get much better help here if you add debugging calls to step through the algorithm and actually draw the paths it is checking (and perhaps the worst-case cost computations). If you can provide images of this, it should be easier to debug.

Also, have you read a good explanation of A*? Are you comfortable with all of the steps, and why they are needed? Can you show that each step is properly represented in your code? If not, I would suggest starting there.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Path finding algorithm for 2d game 2 Answers

Unity2D Grid A* pathfinding freezing for a few seconds 1 Answer

How can I get my pathfinding algorithm to know that it cant go through certain sides of tiles 1 Answer

How to connect generated points on a sphere? 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