-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemyMovement.cs
36 lines (33 loc) · 1008 Bytes
/
EnemyMovement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
//Remember to set all obstacles to "Static" and then Bake navigation to make Pathfinding work.
//Set NavMeshAgent values depending on which kind of enemy.
//Radius 0.3, Speed 3, Stopping Distance 1.3, Height 1.1
[RequireComponent(typeof(NavMeshAgent))]
public class EnemyMovement : MonoBehaviour
{
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
NavMeshAgent nav;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination(player.position);
}
else
{
nav.enabled = false;
}
}
}