I am following a tutorial (namely a survival shooter ), and I am at the implementation stage of NavMesh. Their original script is as follows:
Transform _player;
NavMeshAgent nav;
void Start()
{
_player = GameObject.FindGameObjectWithTag("Player").transform;
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
nav.SetDestination(_player.position);
}
Nothing special yet. I press the game and, oddly enough, the enemy (I have only one at the moment on the stage) reaches the initial position of the player (0,0,0) instead of following him if the player moves. I realized that the player’s position is not updated in the field _player, and it stays on 0,0,0.
I tried a different approach: I dragged the Player game object onto a property in the user interface (first I made the property public, and I changed it to GameObject). In this case, it works flawlessly:
GameObject _player;
NavMeshAgent nav;
void Start()
{
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
nav.SetDestination(_player.transform.position);
}
:
FindGameObjectWithTag GameObject? . , Unity 5.