The script below is attached to the enemy on my game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour {
private Animator attack;
Transform player;
UnityEngine.AI.NavMeshAgent nav;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
nav = GetComponent<UnityEngine.AI.NavMeshAgent> ();
}
The code just below is getting the enemy to stop at a certain distance and shoot from the trigger "Attack". (Below and above are all the same code).
// Update is called once per frame
void Update () {
nav.SetDestination(player.position);
float distance = Vector3.Distance(this.transform.position, player.position);
if (distance < 10f)
{
Animator an = GetComponent<Animator>();
an.SetTrigger("Attack");
}
Debug.Log("distance" + distance);
}
}
This script basically uses the Nav Agent to make the enemy go towards my player and to start a shoot animation when the enemy is at a certain range.
Any help would be great. Thank you.
Aucun commentaire:
Enregistrer un commentaire