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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Follow player using NavMesh.CalculatePath() and CharacterController?

Discussion in 'Scripting' started by SUBZERO8K, Jul 27, 2016.

  1. SUBZERO8K

    SUBZERO8K

    Joined:
    Jan 15, 2013
    Posts:
    36
    Hi, I'm trying to have an AI follow my player using the nav mesh path without using a NavMeshAgent, but a CharacterController instead.

    I have managed to get the path that the AI should follow, but I am struggling to properly make it move along this path towards the player. My current code in the Update function looks like this:

    Code (CSharp):
    1.         elapsed += Time.deltaTime;
    2.         if (elapsed > 0.5f)
    3.         {
    4.             elapsed -= 0.5f;
    5.             NavMesh.CalculatePath(transform.position, player.position, NavMesh.AllAreas, path);
    6.         }
    7.  
    8.  
    9.         Vector3 firstPointDirection = path.corners[1] - transform.position;
    10.  
    11.         controller.Move(new Vector3(firstPointDirection.x, gravity, firstPointDirection.z) * speed * Time.deltaTime);
    Which I thought made sense because it updates so frequently perhaps it should just move towards the first point? However, this just makes the enemy appear to rubber band as in a laggy multiplayer title towards the player. Any suggestions on how to properly do this using the CharacterController Move function? Thank you.
     
  2. SUBZERO8K

    SUBZERO8K

    Joined:
    Jan 15, 2013
    Posts:
    36
    Anyone?
     
  3. foxtarr_unity

    foxtarr_unity

    Joined:
    Mar 27, 2018
    Posts:
    1
    I find it usefull, thanks!