Search Unity

How to detect and eat all pellets?

Discussion in 'Navigation' started by ansuman91225, Jun 18, 2022.

  1. ansuman91225

    ansuman91225

    Joined:
    Jun 18, 2022
    Posts:
    1
    I'm making an AI for pacman. I've placed nodes (the green circles) in the areas where pacman can change directions, and these nodes act as a trigger.

    Here's an image of what it looks like with the nodes in the map - https://drive.google.com/file/d/1qEAoU8tWJBteiFFb6-WWg0onIZbQ57iV/view?usp=sharing

    On trigger, I run this code -

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.   {
    3.     Node node = other.GetComponent<Node>();
    4.  
    5.     if (node != null)
    6.     {
    7.       int index = 0;
    8.       //go after pellets that are in LOS of the node
    9.       while (index < node.availableDirections.Count)
    10.       {
    11.         RaycastHit2D hit = Physics2D.BoxCast(this.transform.position, Vector2.one, 1f, node.availableDirections[index], 19f, pelletLayer);
    12.         if (hit.collider != null && node.availableDirections[index] != -this.movement.direction)
    13.         {
    14.           this.movement.SetDirection(node.availableDirections[index]);
    15.           return;
    16.         }
    17.         index++;
    18.       }
    19.  
    20.       //if there are no pelletes in LOS of node, choose a random direction
    21.       index = Random.Range(0, node.availableDirections.Count);
    22.       if (node.availableDirections[index] != -this.movement.direction && node.availableDirections.Count > 1)
    23.       {
    24.         index++;
    25.  
    26.         // Wrap the index back around if overflowed
    27.         if (index >= node.availableDirections.Count)
    28.         {
    29.           index = 0;
    30.         }
    31.       }
    32.  
    33.       this.movement.SetDirection(node.availableDirections[index]);
    34.     }
    35.   }
    The issue with this code is that pacman doesn't eat all the pellets. It eats around 98% of the pellets, and then moves in a loop. I'm very new to unity, so this is the best I could come up with for pacman eating the pellets. Is there a better way I can find and move toward the pellets?
     
  2. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    278
    Well, there doesn't seem to be an instruction for PacMan to eat pellets until all pellets are eaten, it's instructions are to move around until all pellets are eaten, which does not guarantee that all pellets will be eaten. Also, PacMan keeps moving in a loop because there is no randomization in node.availableDirections[index]. Index it is always zero.

    So. An easy fix would be to either randomize index, or shuffle the available directions before feeding PacMan the first one: that way his movements will be random instead of deterministic, and chances are all pellets will eventually be eaten.

    A second but more complex fix would be to add an A* pathfinding algoritm to your game that allows pacman to actually find a path towards a pellet. This is a bit more involved though and would involve far more setup and code.