Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public abstract class AIBehavior : MonoBehaviour
- {
- public float moveSpeed;
- public float rotSpeed;
- public float fleeDist;
- public float hpFleeThreshold;
- public float stoppingDist;
- protected float currDistP;
- protected float currDistD;
- protected Vector3 destination;
- public GameObject shipAI;
- public GameObject Player;
- public bool isFleeing;
- public void Start()
- {
- shipAI = this.gameObject;
- Player = GameObject.FindWithTag("Player");
- }
- public virtual void CheckDistance()
- {
- currDistP = Vector3.Distance(shipAI.transform.position,Player.transform.position);
- }
- public virtual void MoveToDestination(Vector3 destination)
- {
- this.transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
- Vector3 direction = destination - transform.position;
- float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
- Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
- }
- public virtual void Seek(Vector3 playerLoc)
- {
- if (currDistP >= stoppingDist)
- {
- MoveToDestination(playerLoc);
- }
- }
- public virtual void Flee(Vector3 playerLoc)
- {
- //to get flee point
- //AI position + -1*(Player position - AI position)
- Vector3 fleeDestination = shipAI.transform.position + -1*(playerLoc - shipAI.transform.position);
- //Debug.Log(fleeDestination);
- currDistD = Vector3.Distance(fleeDestination,shipAI.transform.position);
- Debug.Log("flee destination distance:" + currDistD);
- if(currDistD >= stoppingDist)
- {
- MoveToDestination(fleeDestination);
- }
- }
- }
Add Comment
Please, Sign In to add comment