Heroes_of_Hiis/Assets/Project Files/Scripts/Arlo/SlimeAI.cs

155 lines
4.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class SlimeAI : MonoBehaviour
{
Animator animator;
float playerDistance;
GameObject player;
int HP;
public float moveSpeed = 4f;
public float rotSpeed = 100f;
private bool isWandering = false;
private bool isRotatingLeft = false;
private bool isRotatingRight = false;
private bool isWalking = false;
//[SerializeField]
//private UnityEvent onAttack;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
player = GameObject.FindWithTag("Player");
HP = 10;
}
// Update is called once per frame
void Update()
{
playerDistance = Vector3.Distance(player.transform.position, transform.position);
if (playerDistance < 1.5) //Attack
{
animator.SetBool("Wandering", false);
animator.SetBool("EnemyInAttackRange", true);
animator.SetBool("EnemyInAggroRange", true);
animator.SetBool("EnemyInVisionRange", true);
Rotate();
}
else if (playerDistance < 10) //Chase
{
animator.SetBool("Wandering", false);
animator.SetBool("EnemyInAttackRange", false);
animator.SetBool("EnemyInAggroRange", true);
animator.SetBool("EnemyInVisionRange", true);
Rotate();
Move();
}
else if (playerDistance < 18) //Stare
{
animator.SetBool("Wandering", false);
animator.SetBool("EnemyInAttackRange", false);
animator.SetBool("EnemyInAggroRange", false);
animator.SetBool("EnemyInVisionRange", true);
Rotate();
StopCoroutine("Wandering");
}
else
{
animator.SetBool("EnemyInAttackRange", false);
animator.SetBool("EnemyInAggroRange", false);
animator.SetBool("EnemyInVisionRange", false);
}
if (!isWandering)
{
//Idle
StartCoroutine("Wander");
}
Debug.Log(isWandering);
Debug.Log(isRotatingLeft);
Debug.Log(isRotatingRight);
if (isRotatingRight == true)
{
animator.SetBool("Wandering", false);
transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
}
if (isRotatingLeft == true)
{
animator.SetBool("Wandering", false);
transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
}
if (isWalking == true)
{
animator.SetBool("Wandering", true);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
if (HP <= 0)
{
animator.SetBool("Dead", true);
}
if (animator.GetBool("DeathComplete"))
{
print("asd");
Destroy(this.gameObject);
}
}
void Rotate()
{
// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(transform.forward, player.transform.position - transform.position, 1.0f * Time.deltaTime, 0.0f);
// Calculate a rotation a step closer to the target and applies rotation to this object
transform.rotation = Quaternion.LookRotation(newDirection);
}
void Move()
{
transform.position += transform.forward * Time.deltaTime * 1.0f;
}
public void GetHit(int dmg)
{
HP -= dmg;
}
IEnumerator Wander()
{
int rotTime = Random.Range(1, 3);
int rotateWait = Random.Range(1, 5);
int rotateLorR = Random.Range(1, 2);
int walkWait = Random.Range(1, 5);
int walkTime = Random.Range(1, 20);
isWandering = true;
yield return new WaitForSeconds(walkWait);
isWalking = true;
yield return new WaitForSeconds(walkTime);
isWalking = false;
yield return new WaitForSeconds(rotateWait);
if (rotateLorR == 1)
{
isRotatingRight = true;
yield return new WaitForSeconds(rotTime);
isRotatingRight = false;
}
if (rotateLorR == 2)
{
isRotatingLeft = true;
yield return new WaitForSeconds(rotTime);
isRotatingLeft = false;
}
isWandering = false;
}
}