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 SnStarr · Jan 21, 2015 at 06:05 AM · movementaigrid based gameartificial intelligence

Grid Based Movement System AI freezing?

In this code the AI will take a position in a queue to decide which player to attack, then decide the closest player and then decide the one with the lowest health, use path finding to find the shortest route there(Some tiles cost more action points to move through than others), then move towards the player it has decided to attack. But once the AI starts to make its move it freezes in place, and its position queue reverts to ZERO in the inspector. If i manually change its position queue back to 1 it will finish its move but its not just the one AI player, they all will sometimes act right and do what its supposed to do then sometimes in mid move it just freezes for no reason.. A short code snippet from my AIPlayer class. I have gone over it multiple times. It just needs fresh eyes, and any input you can give is helpful. thank you.

 public override void Update () {
         if(GameManager.instance.players[GameManager.instance.currentPlayerIndex] == this) { //If this is current player
     transform.renderer.material.color = Color.green; //Turn its material to green
         } else {
             transform.renderer.material.color = Color.white; //If not current player, turn its material to white
         }
         base.Update();
     }
     
     public override void TurnUpdate ()
     {
         if (positionQueue.Count > 0) { //If the position Queue is greater than 0
             transform.position += (positionQueue[0] - transform.position).normalized * moveSpeed * Time.deltaTime; 
             
             if (Vector3.Distance(positionQueue[0], transform.position) <= 0.1f) {
                 transform.position = positionQueue[0]; 
                 positionQueue.RemoveAt(0); 
                 if (positionQueue.Count == 0) { //if position queue count is at zero
                     actionPoints--; //spend action points
                 }
             }
             
         } else {
             //priority queue
             List<Tile> attacktilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], attackRange, true);
             //List<Tile> movementToAttackTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + attackRange);
             List<Tile> movementTilesInRange = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint + 1000);
             //attack if in range and with lowest HP
             if (attacktilesInRange.Where(x => GameManager.instance.players.Where (y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count () > 0) {
                 var opponentsInRange = attacktilesInRange.Select(x => GameManager.instance.players.Where (y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count () > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList();
                 Player opponent = opponentsInRange.OrderBy (x => x != null ? -x.HP : 1000).First ();
 
                 GameManager.instance.removeTileHighlights(); //remove highlighted tiles once a move destination has been reached
                 moving = false; //make moving false
                 attacking = true; //make attacking true
                 GameManager.instance.highlightTilesAt(gridPosition, Color.red, attackRange); //highlight in red the tiles player can attack based on attack range
 
                 GameManager.instance.attackWithCurrentPlayer(GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y]); //attack the target in the tile that is chose
             }
 
             //move toward nearest opponent
             else if (!moving && movementTilesInRange.Where(x => GameManager.instance.players.Where (y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count() > 0).Count () > 0) {
                 var opponentsInRange = movementTilesInRange.Select(x => GameManager.instance.players.Where (y => y.GetType() != typeof(AIPlayer) && y.HP > 0 && y != this && y.gridPosition == x.gridPosition).Count () > 0 ? GameManager.instance.players.Where(y => y.gridPosition == x.gridPosition).First() : null).ToList();
                 Player opponent = opponentsInRange.OrderBy (x => x != null ? -x.HP : 1000).ThenBy (x => x != null ? TilePathFinder.FindPath(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y],GameManager.instance.map[(int)x.gridPosition.x][(int)x.gridPosition.y]).Count() : 1000).First ();
 
                 GameManager.instance.removeTileHighlights();
                 moving = true;
                 attacking = false;
                 GameManager.instance.highlightTilesAt(gridPosition, Color.blue, movementPerActionPoint, false);
                 
                 List<Tile> path = TilePathFinder.FindPath (GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y],GameManager.instance.map[(int)opponent.gridPosition.x][(int)opponent.gridPosition.y], GameManager.instance.players.Where(x => x.gridPosition != gridPosition && x.gridPosition != opponent.gridPosition).Select(x => x.gridPosition).ToArray());
                 if (path.Count() > 1) {
                     List<Tile> actualMovement = TileHighlight.FindHighlight(GameManager.instance.map[(int)gridPosition.x][(int)gridPosition.y], movementPerActionPoint, GameManager.instance.players.Where(x => x.gridPosition != gridPosition).Select(x => x.gridPosition).ToArray());
                     path.Reverse();
                     if (path.Where(x => actualMovement.Contains(x)).Count() > 0) GameManager.instance.moveCurrentPlayer(path.Where (x => actualMovement.Contains(x)).First());
                 }
             }


Comment
Add comment · Show 1
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 Cherno · Jan 21, 2015 at 09:49 AM 0
Share

the only thing I could recommend is adding Deb.Log lines to see what parts of the script cause problems.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SnStarr · Jan 22, 2015 at 07:54 PM

Actually after several Debug.Log and testing on several different devices and PCs I can say that with the code unchanged as it stands, the problem only comes up on low end PCs and older mobile devices. My Gaming rig, my office Computer and my new Windows Phone all have no problems at all. It must be a memory or graphics card problem. Can't for the life of me figure it out, but as the code works with no problems on higher end devices and PCs, I am not gonna change the code.

Comment
Add comment · 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

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

26 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

Related Questions

If Terrain is larger(width10000Xlength10000) then how to increase the size of the GridGraph of Astar Path Finder 0 Answers

How the heck AI villager movement? 0 Answers

How can I make the I see the player better? 1 Answer

2D sprite movement jagger 1 Answer

without using NavMesh, How to avoid autonomous moving agents/obstacles 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