how can I make it rotate depending on the position of the player?
this is the code of my enemy, how can I make it rotate depending on the position of the player?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public enum EnemyState { Wander, Follow, Die, Attack };
public class EnemyController : MonoBehaviour {
GameObject player;
public EnemyState currState = EnemyState.Wander;
public float range;
public float speed;
public float attackRange;
public float coolDown;
private bool chooseDir = false;
private bool coolDownAttack = false;
private Vector3 randomDir;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
switch (currState)
{
case (EnemyState.Wander):
Wander();
break;
case (EnemyState.Follow):
Follow();
break;
case (EnemyState.Die):
break;
case (EnemyState.Attack):
Attack();
break;
}
if(IsPlayerInRange(range) && currState != EnemyState.Die)
{
currState = EnemyState.Follow;
}
else if(!IsPlayerInRange(range) && currState != EnemyState.Die)
{
currState = EnemyState.Wander;
}
if(Vector3.Distance(transform.position, player.transform.position) <= attackRange)
{
currState = EnemyState.Attack;
}
}
private bool IsPlayerInRange(float range)
{
return Vector3.Distance(transform.position, player.transform.position) <= range;
}
private IEnumerator ChooseDirection()
{
chooseDir = true;
yield return new WaitForSeconds(Random.Range(180f, 180f));
Quaternion nextRotation = Quaternion.Euler(randomDir);
chooseDir = false;
}
void Wander()
{
if (!chooseDir)
{
StartCoroutine(ChooseDirection());
}
if(IsPlayerInRange(range))
{
currState = EnemyState.Follow;
}
}
void Follow()
{
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
}
void Attack()
{
if (!coolDownAttack)
{
GameController.DamagePlayer(1);
StartCoroutine(CoolDown());
}
}
private IEnumerator CoolDown()
{
coolDownAttack = true;
yield return new WaitForSeconds(coolDown);
coolDownAttack = false;
}
}
Comment