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 Tobotis · Jun 13, 2020 at 05:02 PM · fpspathfindinglagprofilerlags

Script causes FPS Drops!

Hi, my A*pathfinding script causes hard FPS Drops. There is a huge bump in the graph in the profiler, which is because of this script:

 void PathProcess()
 {
     foreach (Enemy enemy in enemies)
     {
         if(FindPath(enemy.transform.position, player.transform.position) != null)
         {
             enemy.path = FindPath(enemy.transform.position, player.transform.position);
         }
     }
 }

 public List<Node> FindPath(Vector3 startPos, Vector3 targetPos)
 {
     grid = gameObject.GetComponent<PathfindingGrid>();
     Node startNode = grid.NodeFromWorldPoint(startPos);
     Node targetNode = grid.NodeFromWorldPoint(targetPos);
     List<Node> openNodes = new List<Node>();
     HashSet<Node> closedNodes = new HashSet<Node>();
     openNodes.Add(startNode); 
     while (openNodes.Count > 0)
     {
         
         Node currentNode = openNodes[0];
         for (int i = 1; i < openNodes.Count; i++)
         {
             if(openNodes[i].fCost < currentNode.fCost | openNodes[i].fCost == currentNode.fCost && openNodes[i].hCost < currentNode.hCost)
             {
                 currentNode = openNodes[i];
             }
         }
         openNodes.Remove(currentNode);
         closedNodes.Add(currentNode);
         if(currentNode == targetNode)
         {
             return RetracePath(startNode, targetNode);
         }
         foreach(Node neighbour in grid.GetNeighbours(currentNode))
         {
             
             if (!neighbour.walkable | closedNodes.Contains(neighbour))
             {
                 continue;
             }
             
             int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
             if(newMovementCostToNeighbour < neighbour.gCost || !openNodes.Contains(neighbour))
             {
                 
                 neighbour.gCost = newMovementCostToNeighbour;
                 neighbour.hCost = GetDistance(neighbour, targetNode);
                 
                 neighbour.parent = currentNode;
                 if (!openNodes.Contains(neighbour))
                 {
                     openNodes.Add(neighbour);
                 }
             }
         }
     }
     return RetracePath(startNode, targetNode);



 }
 List<Node> RetracePath(Node startNode, Node endNode)
 {
     path = new List<Node>();
     Node currentNode = endNode;
     while(currentNode != startNode)
     {
         path.Add(currentNode);
         if(currentNode.parent != null)
         {

             currentNode = currentNode.parent;
         }
         else
         {
             break;
         }

     }        
     path.Reverse();
     grid.path = path;
     return path;
     
 }
 int GetDistance(Node a, Node b)
 {
     int distX = Mathf.Abs(a.gridX - b.gridX);
     int distY = Mathf.Abs(a.gridY - b.gridY);
     if (distX > distY)
     {
         return 14 * distY + 10 * (distX - distY);
     }
     else
     {
         return 14 * distX + 10 * (distY - distX);
     }
 }

I use InvokeRepeating("PathProcess", 0.5f, 0.2f) in the Awake Method. The maximum amount of enemies i am spawning is 50. It would be so nice if you could find my problem... Thank You very much!

Comment
Add comment · Show 3
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 Tobotis · Jun 13, 2020 at 05:02 PM 0
Share

Sorry for the horrible formatting of the script :(

avatar image ShadyProductions Tobotis · Jun 13, 2020 at 06:59 PM 0
Share

There are a few things, I mean you find path check if it is not null, then you find path again to assign it to enemy, thats 2 times you try to find a path. I can already guess thats not the best performance there, then there will probably also be a bit inside the findpath aswel that could be more optimized.

avatar image logicandchaos · Jun 13, 2020 at 08:24 PM 0
Share

how often is the script called?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by IvovdMarel · Jun 13, 2020 at 08:17 PM

1) To best find out what's causing the delays, you should use the 'Deep Profile' option. Very often you'll be surprised what's causing it (e.g. you might have something logging to the Console, generally drastically reducing your FPS)

2) As ShadyProductions mentions the algorithm could be optimized. Storing the FindPath result in a variable will make your algorithm twice as fast, and rather than creating two Lists for open and closed nodes, this could just be a property on the node itself that you set,

Comment
Add comment · Show 3 · Share
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 Tobotis · Jun 14, 2020 at 08:05 AM 0
Share

Thank you for your answer... the deep-profiler shows that there is a huge cpu-usage by CouroutonesDelayedCalls (the PathProcessFunction)... I think its because of the InvokeRepeating.

avatar image Tobotis · Jun 14, 2020 at 08:13 AM 0
Share

alt text If i use the update method ins$$anonymous$$d... its the same. I also optimized the script a bit, but i only get 5 FPS more. :( But thank you for your Tips

profiler.png (20.7 kB)
avatar image Tobotis · Jun 14, 2020 at 08:26 AM 0
Share

I dont know how to change the two List into properties, beacuse i have to interate through all open nodes... and this is not possible with the node propery.

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

180 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

Related Questions

Extremely low fps in editor!!! 0 Answers

GFX.WaitForPresent for no reason 0 Answers

Shader.Parse over 1000ms 0 Answers

OnApplicationPause() method works in the start, but doesn't works in the end of the game and something causes the lag. 0 Answers

Optimizing script to improve lag 1 Answer


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