So I was wondering how I could put this script:
public GameObject thePlayer;
public GameObject theEnemy;
public float enemySpeed = 0.01f;
public bool attackTrigger = false;
public bool isAttacking = false;
void Update()
{
transform.LookAt(thePlayer.transform);
if (attackTrigger == false)
{
enemySpeed = 0.01f;
theEnemy.GetComponent<Animation>().Play("walk_in_place");
transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, enemySpeed);
}
if (attackTrigger == true && isAttacking == false)
{
enemySpeed = 0;
theEnemy.GetComponent<Animation>().Play("attack");
StartCoroutine(InflictDamage());
}
}
void OnTriggerEnter()
{
attackTrigger = true;
}
void OnTriggerExit()
{
attackTrigger = false;
}
IEnumerator InflictDamage()
{
isAttacking = true;
yield return new WaitForSeconds(1.1f);
GlobalHealth.currentHealth -= 5;
yield return new WaitForSeconds(0.2f);
isAttacking = false;
}
}
Into This:
void Start()
{
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
theEnemy.GetComponent<Animation>().Play("walk_in_place");
if (distance <= agent.stoppingDistance)
{
//attack
FaceTarget();
}
}
}
void FaceTarget ()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}