Search code examples
unity-game-engineunityscriptpath-finding

Follow GameObject in Unity


Iam newbie in Unity3D, so please help me. :)

I have two objects: Player and Enemy and I need follow Player by Enemy. I tried something like this:

var player : GameObject;

function Start ()
{
    var agent: NavMeshAgent = GetComponent.<NavMeshAgent>();
    agent.SetDestination(player.transform.position);
}
function Update () {

}

but nothing happens.


Solution

  • The Update method run the code every frame, so if you have movement operations put your code in the update method. I have never heard about a NavMeshAgent.

    #1: The easiest way is to add an rigidbody object to your GameObject and use the method MovePosition().

    #2: The second way is to set the gameObject as a child from the gameObject which it should follow. Than unity perform the translations.

    #3: You also can calculate the position by yourself, but first of all you should learn something more about unity.

    Hope that helps!