2022-04-11 15:13:11 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2022-04-18 00:08:12 +00:00
|
|
|
using UnityEngine.Events;
|
2022-04-11 15:13:11 +00:00
|
|
|
|
|
|
|
public class SlimeAI : MonoBehaviour
|
|
|
|
{
|
|
|
|
|
|
|
|
Animator animator;
|
|
|
|
float playerDistance;
|
2022-04-18 00:08:12 +00:00
|
|
|
GameObject player;
|
2022-04-18 15:06:39 +00:00
|
|
|
int HP;
|
2022-04-18 00:08:12 +00:00
|
|
|
|
2022-04-25 14:41:22 +00:00
|
|
|
public float moveSpeed = 4f;
|
2022-04-25 13:34:38 +00:00
|
|
|
public float rotSpeed = 100f;
|
|
|
|
|
|
|
|
private bool isWandering = false;
|
|
|
|
private bool isRotatingLeft = false;
|
|
|
|
private bool isRotatingRight = false;
|
|
|
|
private bool isWalking = false;
|
|
|
|
|
2022-04-25 15:05:53 +00:00
|
|
|
private bool isAttacking = false;
|
|
|
|
private float attackStartTime;
|
2022-04-18 00:08:12 +00:00
|
|
|
//[SerializeField]
|
|
|
|
//private UnityEvent onAttack;
|
|
|
|
|
2022-04-11 15:13:11 +00:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
animator = GetComponent<Animator>();
|
2022-04-18 00:08:12 +00:00
|
|
|
player = GameObject.FindWithTag("Player");
|
2022-05-16 14:47:54 +00:00
|
|
|
HP = 5;
|
2022-05-02 13:09:20 +00:00
|
|
|
|
2022-04-11 15:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2022-04-18 00:08:12 +00:00
|
|
|
playerDistance = Vector3.Distance(player.transform.position, transform.position);
|
2022-04-25 15:05:53 +00:00
|
|
|
|
|
|
|
if (HP <= 0)
|
|
|
|
{
|
|
|
|
animator.Play("Die");
|
|
|
|
}
|
2022-04-18 00:08:12 +00:00
|
|
|
if (playerDistance < 1.5) //Attack
|
|
|
|
{
|
2022-04-25 14:41:22 +00:00
|
|
|
animator.SetBool("Wandering", false);
|
2022-04-18 00:08:12 +00:00
|
|
|
animator.SetBool("EnemyInAttackRange", true);
|
|
|
|
animator.SetBool("EnemyInAggroRange", true);
|
|
|
|
animator.SetBool("EnemyInVisionRange", true);
|
|
|
|
Rotate();
|
2022-04-25 15:05:53 +00:00
|
|
|
Attack();
|
2022-04-18 00:08:12 +00:00
|
|
|
}
|
2022-04-25 13:34:38 +00:00
|
|
|
else if (playerDistance < 10) //Chase
|
2022-04-18 00:08:12 +00:00
|
|
|
{
|
2022-04-25 14:41:22 +00:00
|
|
|
animator.SetBool("Wandering", false);
|
2022-04-18 00:08:12 +00:00
|
|
|
animator.SetBool("EnemyInAttackRange", false);
|
|
|
|
animator.SetBool("EnemyInAggroRange", true);
|
|
|
|
animator.SetBool("EnemyInVisionRange", true);
|
|
|
|
Rotate();
|
|
|
|
Move();
|
|
|
|
}
|
2022-04-25 13:34:38 +00:00
|
|
|
else if (playerDistance < 18) //Stare
|
2022-04-18 00:08:12 +00:00
|
|
|
{
|
2022-04-25 14:41:22 +00:00
|
|
|
animator.SetBool("Wandering", false);
|
2022-04-18 00:08:12 +00:00
|
|
|
animator.SetBool("EnemyInAttackRange", false);
|
|
|
|
animator.SetBool("EnemyInAggroRange", false);
|
|
|
|
animator.SetBool("EnemyInVisionRange", true);
|
|
|
|
Rotate();
|
2022-04-25 14:41:22 +00:00
|
|
|
StopCoroutine("Wandering");
|
2022-04-18 00:08:12 +00:00
|
|
|
}
|
2022-04-25 14:41:22 +00:00
|
|
|
else
|
2022-04-18 00:08:12 +00:00
|
|
|
{
|
|
|
|
animator.SetBool("EnemyInAttackRange", false);
|
|
|
|
animator.SetBool("EnemyInAggroRange", false);
|
|
|
|
animator.SetBool("EnemyInVisionRange", false);
|
2022-04-25 14:41:22 +00:00
|
|
|
}
|
2022-05-02 13:09:20 +00:00
|
|
|
|
2022-04-25 14:41:22 +00:00
|
|
|
if (!isWandering)
|
|
|
|
{
|
|
|
|
//Idle
|
2022-04-25 13:34:38 +00:00
|
|
|
StartCoroutine("Wander");
|
2022-04-25 14:41:22 +00:00
|
|
|
}
|
|
|
|
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;
|
2022-04-18 00:08:12 +00:00
|
|
|
}
|
2022-05-02 11:28:44 +00:00
|
|
|
|
|
|
|
|
2022-04-25 13:34:38 +00:00
|
|
|
|
2022-04-18 00:08:12 +00:00
|
|
|
}
|
|
|
|
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;
|
2022-04-11 15:13:11 +00:00
|
|
|
}
|
2022-04-18 15:06:39 +00:00
|
|
|
|
|
|
|
public void GetHit(int dmg)
|
|
|
|
{
|
|
|
|
HP -= dmg;
|
|
|
|
}
|
2022-04-25 15:05:53 +00:00
|
|
|
|
|
|
|
void Attack()
|
|
|
|
{
|
|
|
|
if (!isAttacking)
|
|
|
|
{
|
|
|
|
isAttacking = true;
|
|
|
|
attackStartTime = Time.time;
|
|
|
|
StartCoroutine(DelayedAttack());
|
|
|
|
}
|
|
|
|
else if (Time.time - attackStartTime > 0.833)
|
|
|
|
{
|
|
|
|
isAttacking = false;
|
|
|
|
attackStartTime = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator DelayedAttack()
|
|
|
|
{
|
|
|
|
yield return new WaitForSeconds(0.2f);
|
|
|
|
Debug.Log("Attack");
|
2022-05-16 14:47:54 +00:00
|
|
|
PlayerInfo.Instance.AddHealth(-1);
|
2022-04-25 15:05:53 +00:00
|
|
|
//do attack e.g check for player in range/hit collider
|
|
|
|
}
|
2022-04-25 13:34:38 +00:00
|
|
|
|
|
|
|
IEnumerator Wander()
|
|
|
|
{
|
|
|
|
int rotTime = Random.Range(1, 3);
|
2022-04-25 14:41:22 +00:00
|
|
|
int rotateWait = Random.Range(1, 5);
|
2022-04-25 13:34:38 +00:00
|
|
|
int rotateLorR = Random.Range(1, 2);
|
|
|
|
int walkWait = Random.Range(1, 5);
|
2022-04-25 14:41:22 +00:00
|
|
|
int walkTime = Random.Range(1, 20);
|
2022-04-25 13:34:38 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-02 13:09:20 +00:00
|
|
|
}
|