Placido_GDD

AIBehavior

Nov 26th, 2021 (edited)
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1.  
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6.  
  7. public abstract class AIBehavior : MonoBehaviour
  8. {
  9.     public float moveSpeed;
  10.     public float rotSpeed;
  11.     public float fleeDist;
  12.     public float hpFleeThreshold;
  13.     public float stoppingDist;
  14.     protected float currDistP;
  15.     protected float currDistD;
  16.     protected Vector3 destination;
  17.     public GameObject shipAI;
  18.     public GameObject Player;
  19.     public bool isFleeing;
  20.  
  21.     public void Start()
  22.     {
  23.         shipAI = this.gameObject;
  24.         Player = GameObject.FindWithTag("Player");
  25.     }
  26.  
  27.     public virtual void  CheckDistance()
  28.     {
  29.         currDistP = Vector3.Distance(shipAI.transform.position,Player.transform.position);
  30.        
  31.        
  32.        
  33.     }
  34.  
  35.     public virtual void MoveToDestination(Vector3 destination)
  36.     {
  37.         this.transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
  38.         Vector3 direction = destination - transform.position;
  39.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  40.         Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  41.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
  42.     }
  43.     public virtual void Seek(Vector3 playerLoc)
  44.     {
  45.         if (currDistP >= stoppingDist)
  46.         {
  47.             MoveToDestination(playerLoc);
  48.         }
  49.     }
  50.    
  51.     public virtual void Flee(Vector3 playerLoc)
  52.     {
  53.         //to get flee point
  54.         //AI position + -1*(Player position - AI position)
  55.         Vector3 fleeDestination = shipAI.transform.position + -1*(playerLoc - shipAI.transform.position);
  56.         //Debug.Log(fleeDestination);
  57.         currDistD = Vector3.Distance(fleeDestination,shipAI.transform.position);
  58.         Debug.Log("flee destination distance:" + currDistD);
  59.         if(currDistD >= stoppingDist)
  60.         {
  61.             MoveToDestination(fleeDestination);
  62.         }
  63.     }    
  64. }
  65.  
Add Comment
Please, Sign In to add comment