2022-04-18 03:08:12 +03:00

69 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class SlimeAI : MonoBehaviour
{
Animator animator;
float playerDistance;
GameObject player;
//[SerializeField]
//private UnityEvent onAttack;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
player = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void Update()
{
playerDistance = Vector3.Distance(player.transform.position, transform.position);
if (playerDistance < 1.5) //Attack
{
animator.SetBool("EnemyInAttackRange", true);
animator.SetBool("EnemyInAggroRange", true);
animator.SetBool("EnemyInVisionRange", true);
Rotate();
}
else if (playerDistance < 5) //Chase
{
animator.SetBool("EnemyInAttackRange", false);
animator.SetBool("EnemyInAggroRange", true);
animator.SetBool("EnemyInVisionRange", true);
Rotate();
Move();
}
else if (playerDistance < 8) //Stare
{
animator.SetBool("EnemyInAttackRange", false);
animator.SetBool("EnemyInAggroRange", false);
animator.SetBool("EnemyInVisionRange", true);
Rotate();
}
else //Idle
{
animator.SetBool("EnemyInAttackRange", false);
animator.SetBool("EnemyInAggroRange", false);
animator.SetBool("EnemyInVisionRange", false);
}
}
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;
}
}