Search code examples
c#unity-game-enginenavmesh

How to use a prefab gameobject clone (Character Selection) as a player's target for a Navmesh Agent? How Navmesh Agent keep following the player


I'm new to Unity. I also new to Stackoverflow. Nice to meet you all!

I am using Unity version 2021.3 LTS

Here the details of my problem :

  • I still haven't found how to make navmesh agent to detect and follow a prefab clone.
  • So I'm using Character Selection Scene and then spawned into Scene Game.
  • I wanted an animal to follow our chosen character once it was loaded into the scene (for testing)
  • The animal is not following again when player move around

What did I try:

  • I have read some videos/articles/thread that I found. So I used method which the tag ("Player") and I used NavMesh Agent on the Animal. Also I put the tag for all characters
  • At first I thought the animal success followed the player, but it seems stopped at the position where the player spawned. The animal is not following again when player moving

What were I expecting :

*How can we make the animal keep following player that a clone?*

I'm using debug.log for targetPlayer.name. It identified my selected character's name that spawned at scene Game.

Here my Code NPC_Follow.cs , I attached it on the animal

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
 
public class NPC_Follow : MonoBehaviour
{
 
    private GameObject targetPlayer;
 
    NavMeshAgent myAgent;
 
    // Start is called before the first frame update
    void Start()
    {
        targetPlayer = GameObject.FindGameObjectWithTag("Player");
 
        Debug.Log("Name: " + targetPlayer.name);
 
        myAgent = GetComponent<NavMeshAgent>();
    }
 
    // Update is called once per frame
    void Update()
    {
        myAgent.destination = targetPlayer.transform.position;
    }
}

I also put LoadCharacter.cs if needed to see. LoadCharacter.cs, attached to empty gameobject at game scene. for spawning the selected character

public class LoadCharacter : MonoBehaviour
{
 
    public GameObject[] characterPrefabs;
    public Transform playerStartPosition;
    private string selectedCharacterDataName = "SelectedCharacter";
    int selectedCharacter;
    public GameObject playerObject;
 
 
 
    // Start is called before the first frame update
    void Start()
    {
        selectedCharacter = PlayerPrefs.GetInt(selectedCharacterDataName, 0);
        playerObject = Instantiate(characterPrefabs[selectedCharacter],
                                 playerStartPosition.position,
                                 characterPrefabs[selectedCharacter].transform.rotation);
    }
}

Thank you and have a nice day!


Solution

  • To answer my question, I found solution

    Debugging :

    • When I tested using Debug.Log Destination to that targetPlayer position. it remain same. even after the player moves.

    What I did thinking :

    • it is because Empty GameObject parent that have child gameobject contain actual player?
    • because the debug.log destination position is same as Empty Parent's position. that Empty Parent also have tag "Player"

    What I did:

    • reference to that child gameobject instead, rather than tag "player". since that child have actual player. It's success following!

    Transform childTransform = playerCharacter.transform.Find("Player Name");

    I will test later if I could try change tag EmptyGameObject Parent to none or any. so it could try searching where child gameobject that have tag "player"