- Home /
ENEMY MOVEMENT SCRIPT
hi my goal with this script is to get my enemy to chase my player so far this script is not working at all. There are no errors but my enemy dosen't chase my player the enemy just stands still. I'm am not sure how to get it to work please help.
using UnityEngine; using System.Collections; using UnityEngine.AI;
public class Enemymovement : MonoBehaviour { private bool pursue; float firingPause; //public GameObject Subo; Subhealth submarinehealth; EnemyHealth enemysub; public int Enemyspeed = 100; private Transform Sub; //this is so the enemey sub will move towards the player but it obivoulsy isnt in code yet so the enemy wont move until put in code NavMeshAgent NavAgent; private Transform Player; //you need this for nav mesh //Transform SubPosition; // Use this for initialization
void Start ()
{
NavAgent = GetComponent <NavMeshAgent> ();
Sub = GameObject.FindGameObjectWithTag ("Player").transform; // this sets it to find the player sub but not attack we could change the sub to chase a AI or game object if we want to
//NavAgent = GetComponent <NavMeshAgent> ();
}
// Update is called once per frame
void FixedUpdate ()
{
MovetowardsPlayer ();
}
void MovetowardsPlayer () {
if (Sub != null) { //! null means if theres not a player to go towards then enemy does nothing
SetNavdestination (Sub);
}
}
public void SetNavdestination (Transform Sub) {
NavAgent.SetDestination (Sub.position); //navAgent is the nav mesh and its just asking where do you want to go we put Sub position and it tells the nav mesh it wants to go towards the sub/player
}
}