Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Enemy tracking

Discussion in 'Scripting' started by ryanhagedorn, May 18, 2020.

  1. ryanhagedorn

    ryanhagedorn

    Joined:
    May 15, 2020
    Posts:
    5
    I am having trouble getting my enemy to move towards the player as of right now it moves away what do I need to change in order to fix it?
    Code (CSharp):
    1. public class enemy : MonoBehaviour
    2. {
    3.     Transform playerTransform;
    4.     UnityEngine.AI.NavMeshAgent myNavmesh;
    5.     public float checkRate = 0.001f;
    6.     float nextCheck;
    7.    
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         if (GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
    12.             playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    13.  
    14.         myNavmesh = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        if (Time.time > nextCheck)
    21.         {
    22.             nextCheck = Time.time + checkRate;
    23.             FollowPlayer();
    24.         }
    25.     }
    26.     void FollowPlayer()
    27.     {
    28.         myNavmesh.transform.LookAt(playerTransform);
    29.         myNavmesh.destination = playerTransform.position;
    30.     }
    31.  
    32. }
    33.  
     
  2. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    637
    you should probably use
    myNavmesh.SetDestination()
    instead of
    myNavmesh.destination =
    , and maybe the rotation makes him move in the wrong direction, so put the following code in your Start() method:
    myNavmesh.updateRotation = false;
     
  3. ryanhagedorn

    ryanhagedorn

    Joined:
    May 15, 2020
    Posts:
    5
    I changed the code like you suggested put I’m still having trouble getting it to work could it have something to do with the asset that I am using which is tagged as player?
     
  4. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    637
    Depends on the Asset ... Outcomment the following lines in your Start function

    Code (CSharp):
    1. if (GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
    2.             playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    3.         myNavmesh = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
    make the variables "playerTransform" and "myNavmesh" visible in the inspector by making them public or adding the [SerializeField] attribute, then assign them manually.
    You can assign an Empty GameObject instead of the PlayerTransform to check the behaviour - also check if the navmesh is generated correctly, re-bake it maybe.