Search Unity

Resolved My AI isn't working help me pls

Discussion in 'ML-Agents' started by Muffin_Cat, Apr 10, 2021.

  1. Muffin_Cat

    Muffin_Cat

    Joined:
    Dec 18, 2020
    Posts:
    9
    hello I have a problem with my ai system he is not moving at all or doing anything and I have a navmesh surface in my game what can I do? pls help me

    here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3.  
    4. public class AIController : MonoBehaviour
    5. {
    6.     public NavMeshAgent agent;
    7.     public Transform player;
    8.     public GameObject projectile;
    9.  
    10.     public LayerMask whatIsGround, whatIsPlayer;
    11.     public Vector3 walkPoint;
    12.  
    13.     public float walkPointRange;
    14.     public float timeBetweenAttacks;
    15.     public float sightRange, attackRange;
    16.     public float health;
    17.  
    18.     public bool playerInSightRange, playerInAttackRange;
    19.  
    20.     private bool walkPointSet;
    21.     private bool alreadyAttacked;
    22.  
    23.     private void Awake()
    24.     {
    25.         player = GameObject.Find("PlayerManager").transform;
    26.         agent = GetComponent<NavMeshAgent>();
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         Debug.DrawRay(transform.position + transform.up / 4.5f, transform.forward * 5, Color.green, 2f, false);
    32.  
    33.         playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
    34.         playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
    35.  
    36.         if (!playerInSightRange && !playerInAttackRange) Patroling();
    37.         if (playerInSightRange && !playerInAttackRange) ChasePlayer();
    38.         if (playerInAttackRange && playerInSightRange) AttackPlayer();
    39.     }
    40.  
    41.     private void Patroling()
    42.     {
    43.         if (!walkPointSet) SearchWalkPoint();
    44.  
    45.         if (walkPointSet)
    46.             agent.SetDestination(walkPoint);
    47.  
    48.         Vector3 distanceToWalkPoint = transform.position - walkPoint;
    49.  
    50.         if (distanceToWalkPoint.magnitude < 1f)
    51.             walkPointSet = false;
    52.     }
    53.  
    54.     private void SearchWalkPoint()
    55.     {
    56.         float randomZ = Random.Range(-walkPointRange, walkPointRange);
    57.         float randomX = Random.Range(-walkPointRange, walkPointRange);
    58.  
    59.         walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
    60.  
    61.         if (Physics.Raycast(walkPoint, -transform.up, -2, whatIsGround))
    62.             walkPointSet = true;
    63.     }
    64.  
    65.     private void ChasePlayer()
    66.     {
    67.         agent.SetDestination(player.position);
    68.     }
    69.  
    70.     private void AttackPlayer()
    71.     {
    72.         agent.SetDestination(transform.position);
    73.  
    74.         transform.LookAt(player);
    75.  
    76.         if (!alreadyAttacked)
    77.         {
    78.             Rigidbody rb = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Rigidbody>();
    79.  
    80.             rb.AddForce(transform.forward * 32f, ForceMode.Impulse);
    81.             rb.AddForce(transform.up * 32f, ForceMode.Impulse);
    82.  
    83.             alreadyAttacked = true;
    84.             Invoke(nameof(ResetAttack), timeBetweenAttacks);
    85.         }
    86.     }
    87.  
    88.     private void ResetAttack()
    89.     {
    90.         alreadyAttacked = false;
    91.     }
    92.  
    93.     public void TakeDamage(int damage)
    94.     {
    95.         health -= damage;
    96.  
    97.         if (health <= 0) Invoke(nameof(DestroyEnemy), .5f);
    98.     }
    99.  
    100.     private void DestroyEnemy()
    101.     {
    102.         Destroy(gameObject);
    103.     }
    104.  
    105.     private void OnDrawGizmosSelected()
    106.     {
    107.         Gizmos.color = Color.red;
    108.         Gizmos.DrawWireSphere(transform.position, attackRange);
    109.         Gizmos.color = Color.white;
    110.         Gizmos.DrawWireSphere(transform.position, sightRange);
    111.     }
    112. }
     
  2. andrewcoh_unity

    andrewcoh_unity

    Unity Technologies

    Joined:
    Sep 5, 2019
    Posts:
    162
    I'm guessing this is a component that is used by a separate Agent script?

    Are there any console error logs or timeouts occurring? Does your agent have a DecisionRequester component on it? Please share more information about what is happening so that I can give you a better answer.
     
  3. Muffin_Cat

    Muffin_Cat

    Joined:
    Dec 18, 2020
    Posts:
    9
    never mined I fixed that already
     
  4. Myalow

    Myalow

    Joined:
    Sep 18, 2021
    Posts:
    1
    how did you fixed that? I have similar problem