Advertisement
EmmyDev

Patrik AI

Dec 29th, 2022 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6. using Random=UnityEngine.Random;
  7.  
  8. public class PatrikAI : MonoBehaviour
  9. {
  10.     public GameObject player;
  11.     public Transform eyes;
  12.     public float speed = 3f;
  13.     public NavMeshAgent agent;
  14.     public GameObject[] wanderLocations;
  15.     public bool isHunting;
  16.     public bool pickOne = true;
  17.     void Update()
  18.     {
  19.         agent.speed = speed;
  20.         RaycastHit hit;
  21.  
  22.         Vector3 fromPosition = eyes.transform.position;
  23.         Vector3 toPosition = player.transform.position;
  24.         Vector3 direction = toPosition - fromPosition;
  25.         if (Physics.Raycast(eyes.transform.position, direction, out hit, Mathf.Infinity))
  26.         {
  27.             if(hit.transform.tag == "Player")
  28.             {
  29.                 Hunt();
  30.             }
  31.             else if(player.GetComponent<PlayerActions>().isHidden == true)
  32.             {
  33.                 if(isHunting == true)
  34.                 {
  35.                     Hunt();
  36.                 }
  37.                 else
  38.                 {
  39.                     Hear();
  40.                 }
  41.                
  42.             }
  43.             else
  44.             {
  45.                 Hear();
  46.             }
  47.            
  48.         }
  49.     }
  50.     void Follow(){
  51.         isHunting = false;
  52.         speed = 4f;
  53.         agent.destination = player.transform.position;
  54.     }
  55.     void Hunt(){
  56.         isHunting = true;
  57.         speed = 8f;
  58.         agent.destination = player.transform.position;
  59.     }
  60.     void WanderOff(){
  61.         speed = 4f;
  62.        
  63.         if(pickOne == true)
  64.         {
  65.             agent.destination = wanderLocations[Random.Range(0, wanderLocations.Length - 1)].transform.position;
  66.             pickOne = false;
  67.         }
  68.         if(agent.remainingDistance < 3f && pickOne == false){
  69.             agent.destination = wanderLocations[Random.Range(0, wanderLocations.Length - 1)].transform.position;
  70.             pickOne = true;
  71.        
  72.         }
  73.        
  74.     }
  75.     void Hear(){
  76.         isHunting = false;
  77.         GameObject[] noice = GameObject.FindGameObjectsWithTag("Noice");
  78.         if(noice.Length == 2){
  79.             Destroy(noice[0]);
  80.         }
  81.  
  82.         if(noice.Length > 0){
  83.             speed = 8f;
  84.             agent.destination = noice[noice.Length - 1].transform.position;
  85.         }
  86.         else
  87.         {
  88.             WanderOff();
  89.         }
  90.        
  91.        
  92.     }
  93. }
  94.  
  95.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement