Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question A* 2D pathfinding trouble

Discussion in 'Navigation' started by BartyGames3, Jun 27, 2023.

  1. BartyGames3

    BartyGames3

    Joined:
    Jul 16, 2015
    Posts:
    27
    Hi, so I created this simple zombie script for my asset that I have been working on, but now I am trying to incorporate A* pathfinding for a personal project. The issue is, The zombies are continuing to not move to the target destination. This is the zombie AI script attached. The video I'm attaching also shows all of the hierarchy and attached components.

    I'm just quite confused why the zombie is not moving toward the player along the path, and any help or pointers in the right direction would be much appreciated.

    P.S. I am also using photon engine.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using UnityEngine.AI;
    6. using Pathfinding;
    7.  
    8. public class ZombieAI : MonoBehaviour
    9. {
    10.     PhotonView view;
    11.     private AIPath path;
    12.     [HideInInspector] public Seeker seeker;
    13.  
    14.     [Header("Variables")]
    15.     public float speed = 3f;
    16.     public float damage = 5f;
    17.     public float health = 100f;
    18.  
    19.     [Header("Zombie Sprites (randomly chooses)")]
    20.     public Sprite[] zombieRenders; // Selects a sprite from the list on start.
    21.     SpriteRenderer spriteRenderer;
    22.  
    23.     private Transform target;
    24.     private Animator anim;
    25.     private Rigidbody2D rb;
    26.  
    27.     float randomRot;
    28.     Quaternion rotate;
    29.  
    30.     bool isAttacking = false;
    31.     bool isAttackCoroRunning = false;
    32.  
    33.     private void Start()
    34.     {
    35.         seeker = GetComponent<Seeker>();
    36.         path = GetComponent<AIPath>();
    37.         isAttacking = false;
    38.         view = GetComponent<PhotonView>();
    39.         rb = GetComponent<Rigidbody2D>();
    40.         spriteRenderer = GetComponentInChildren<SpriteRenderer>();
    41.         anim = GetComponentInChildren<Animator>();
    42.  
    43.         view.RPC(nameof(SelectZombieSprite), RpcTarget.AllViaServer);
    44.     }
    45.  
    46.     [PunRPC]
    47.     void SelectZombieSprite()
    48.     {
    49.         if (PhotonNetwork.IsMasterClient)
    50.         {
    51.             // Controls the random sprite to select
    52.             int index = Random.Range(0, zombieRenders.Length);
    53.             spriteRenderer.sprite = zombieRenders[index];
    54.             path.maxSpeed = 6;
    55.             path.maxAcceleration = 2;
    56.  
    57.             // Controls random rotation
    58.             randomRot = Random.Range(1, 360);
    59.             transform.rotation = Quaternion.Euler(0f, 0f, randomRot);
    60.             anim.SetBool("isMoving", true);
    61.         }
    62.     }
    63.  
    64.     public void OnTriggerEnter2D(Collider2D other)
    65.     {
    66.         // Sets the target of which player entered the range
    67.         if (other.CompareTag("Player"))
    68.         {
    69.             target = other.transform;
    70.         }
    71.     }
    72.  
    73.     public void OnTriggerExit2D(Collider2D other)
    74.     {
    75.         // Removes the target of which player entered the range
    76.         if (other.CompareTag("Player"))
    77.         {
    78.             target = null;
    79.             isAttacking = false;
    80.         }
    81.     }
    82.  
    83.  
    84.     public void Update()
    85.     {
    86.         if (target == null)
    87.         {
    88.             anim.SetBool("isMoving", true);
    89.             return;
    90.         }
    91.  
    92.         if (target.GetComponent<StatsManager>().health <= 0)
    93.         {
    94.             target = null;
    95.             return;
    96.         }
    97.  
    98.         float dist = Vector2.Distance(transform.position, target.position);
    99.  
    100.         if (dist <= 0.7f)
    101.         {
    102.             // Zombie is within attack range
    103.             if (!isAttacking)
    104.             {
    105.                 StartCoroutine(DealDamage());
    106.                 isAttacking = true;
    107.             }
    108.  
    109.             // Stop moving
    110.             seeker.StartPath(transform.position, transform.position);
    111.             anim.SetBool("isMoving", true);
    112.         }
    113.         else
    114.         {
    115.             // Zombie is outside attack range, continue moving
    116.  
    117.             seeker.StartPath(this.transform.position, target.transform.position);
    118.             anim.SetBool("isMoving", false);
    119.  
    120.             // Look towards the player
    121.             transform.right = target.position;
    122.         }
    123.     }
    124.  
    125.  
    126.  
    127.  
    128.     public IEnumerator DealDamage()
    129.     {
    130.         // Handles the attacking of the zombie. It has a timer which can be changed on line 114.
    131.         yield return new WaitForSeconds(.1f);
    132.         if (isAttacking)
    133.         {
    134.             target.GetComponent<StatsManager>().TakeDamage();
    135.             yield return new WaitForSeconds(1f);
    136.             StartCoroutine(DealDamage());
    137.         }
    138.         else if(!isAttacking)
    139.         {
    140.             StopAllCoroutines();
    141.         }
    142.  
    143.     }
    144.  
    145.     public void TakeDamage()
    146.     {
    147.         // Calls the networked function over the network to take damage(RPC). The damage is only handled by the master client.
    148.         view.RPC(nameof(TakeDamageRPC), RpcTarget.MasterClient);
    149.     }
    150.  
    151.     [PunRPC]
    152.     void TakeDamageRPC()
    153.     {
    154.         // Handles damage over the network.
    155.         health -= 25f;
    156.  
    157.         if (health <= 0)
    158.         { PhotonNetwork.Destroy(gameObject); }
    159.     }
    160.  
    161. }
    162.  
     
  2. BartyGames3

    BartyGames3

    Joined:
    Jul 16, 2015
    Posts:
    27
    Figured out the issue, under AI path it needed to be set to YAxisForward.