Advertisement
VizKa

Stealth game AI (Unity)

May 6th, 2023
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Transactions;
  5. using UnityEngine;
  6. using UnityEngine.AI;
  7. using UnityEngine.SceneManagement;
  8.  
  9. public class SimpleAI : MonoBehaviour
  10. {
  11.     [Header("General Variables")]
  12.     [SerializeField] private GameObject player;
  13.  
  14.    
  15.     [Header("Movement Settings")]
  16.     [SerializeField] private float defaultSpeed = 5f;
  17.     [SerializeField] private float howCloseToPatrollingPoint = 0.1f;
  18.     [SerializeField] private float accelerationWhenChasingPlayer = 40;
  19.     [SerializeField] private float speedWhenChasingPlayer = 5;
  20.     [SerializeField] private Transform[] patrollingPoints;
  21.     private int patrollingCounter = 1;
  22.     private int numberOfPointsToPatrol;
  23.     private NavMeshAgent myAgent;
  24.     private bool reachedDestination;
  25.     private float defaultAcceleration = 40;
  26.  
  27.     [Header("Player in range Settings")]
  28.     [SerializeField] private float distanceToKeepFromPlayer = 3.0f;
  29.     private FieldOfView myFieldOfView;
  30.  
  31.     [Header("Health system Settings")]
  32.     [SerializeField] public int health = 100;
  33.  
  34.     [Header("Attack Settings")]
  35.     [SerializeField] private int damage = 20;
  36.    
  37.  
  38.     // Start is called before the first frame update
  39.     private void Start()
  40.     {
  41.         myAgent = this.GetComponent<NavMeshAgent>();
  42.         myAgent.speed = defaultSpeed;
  43.         numberOfPointsToPatrol = patrollingPoints.Length;
  44.         myFieldOfView = this.GetComponent<FieldOfView>();
  45.         player = GameObject.FindGameObjectWithTag("Player");
  46.        
  47.  
  48.         GoToNextPoint();
  49.        
  50.     }
  51.  
  52.     private void FixedUpdate()
  53.     {
  54.         if (!myAgent.enabled) return;
  55.        
  56.         if (myFieldOfView.canSeePlayer && !player.GetComponent<Camo>().playerHidden)
  57.         {
  58.             ChasePlayer();
  59.         }
  60.         else
  61.         {
  62.             Patrolling();
  63.         }
  64.  
  65.     }
  66.  
  67.     private void ChasePlayer()
  68.     {
  69.         //set the player as destination and as thing to look
  70.         this.transform.LookAt(player.transform.position);
  71.         myAgent.SetDestination(player.transform.position);
  72.  
  73.         //set variables for the chasing player case
  74.         myAgent.autoBraking = true;
  75.         myAgent.speed = speedWhenChasingPlayer;
  76.         myAgent.acceleration = accelerationWhenChasingPlayer;
  77.  
  78.         if (myAgent.remainingDistance < distanceToKeepFromPlayer)
  79.         {
  80.             myAgent.isStopped = true;
  81.             //Time.timeScale = 0;
  82.             player.GetComponent<rigidbody2>().enabled = false;
  83.             StartCoroutine(ExecuteAfterTime(2f));
  84.         }
  85.         else
  86.         {
  87.             myAgent.isStopped = false;
  88.         }
  89.     }
  90.    
  91.     private void GoToNextPoint()
  92.     {
  93.         if (numberOfPointsToPatrol == 0)
  94.             return;
  95.        
  96.         // Disabling auto-braking allows for continuous movement
  97.         // between points (ie, the agent doesn't slow down as it
  98.         // approaches a destination point).
  99.         myAgent.autoBraking = false;
  100.  
  101.         myAgent.SetDestination(patrollingPoints[patrollingCounter].position);
  102.  
  103.         patrollingCounter = (patrollingCounter + 1) % numberOfPointsToPatrol;
  104.  
  105.     }
  106.  
  107.     private void Patrolling()
  108.     {
  109.        
  110.         myAgent.speed = defaultSpeed;
  111.         myAgent.acceleration = defaultAcceleration;
  112.        
  113.         if (!myAgent.pathPending && myAgent.remainingDistance < howCloseToPatrollingPoint)
  114.         {
  115.             GoToNextPoint();
  116.         }
  117.     }
  118.  
  119.     IEnumerator ExecuteAfterTime(float time)
  120.     {
  121.         yield return new WaitForSeconds(time);
  122.         SceneManager.LoadScene(2);
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement