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

Question NPC Follow Roads HELP!

Discussion in 'Navigation' started by JeilyFan124, Oct 5, 2022.

  1. JeilyFan124

    JeilyFan124

    Joined:
    May 10, 2020
    Posts:
    11
    So, I need help making NPCs follow a road to a random destination. I have multiple roads ( all tagged roads) I need help finding a way for them to find and then follow a road and change roads if needed to get to destination. NPCs will spawn at different locations. Also, I can't use NavMesh, because something about my map won't allow it so I have to do it manually. Any Help?

    Here is my code:

    Code (CSharp):
    1. public Animator animator;
    2.  
    3.     public GameObject target;
    4.     public GameObject destination;
    5.     public float speed = 2f;
    6.     Rigidbody rig;
    7.  
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         rig = GetComponent<Rigidbody>();
    13.  
    14.         animator = GetComponent<Animator>();
    15.  
    16.         target = GameObject.FindWithTag("Roads");
    17.  
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     private void FixedUpdate()
    23.     {
    24.         Vector3 pos = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.fixedDeltaTime);
    25.         rig.MovePosition(pos);
    26.     }
    27.  
    28.     public void OnCollisionEnter(Collision collision)
    29.     {
    30.  
    31.  
    32.  
    33.         if (collision.gameObject.tag == "Roads")
    34.         {
    35.             RandomDestination();
    36.             Vector3 pos = Vector3.MoveTowards(transform.position, destination.transform.position, speed * Time.fixedDeltaTime);
    37.             rig.MovePosition(pos);
    38.         }
    39.  
    40.  
    41.  
    42.     }
    43.  
    44.     public void RandomDestination()
    45.     {
    46.         int Des = Random.Range(0, 14);
    47.  
    48.         if (Des == 0)
    49.         {
    50.             destination = GameObject.Find("AionaTailoring");
    51.         }
    52.         else if (Des == 1)
    53.         {
    54.             destination = GameObject.Find("Clinic");
    55.         }
    56.         else if (Des == 2)
    57.  
    58. You get the point after this.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,456
    Either fix the navmesh issue or make your own solution. Maybe using splines?
     
  3. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    1,650
    You have to fix your navmesh problem or youll be pulling your hair out till your bald...
     
    DevDunk likes this.
  4. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    +1 to that
    It really sounds like you need to learn all the basics of using a navmesh in unity
    I'd advise these videos as they cover everything you need to know to get started.
     
  5. warthos3399

    warthos3399

    Joined:
    May 11, 2019
    Posts:
    1,650
    You quoted "I can't use NavMesh, because something about my map won't allow it". Ok, are you using a terrain or a mesh?. We need more info to help...
     
    BluezamX likes this.
  6. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    Agreed. I'd say make a new scene with just a flat plane and try baking a navmesh on that (trust me it'll work ;-)
    Next to thing to try is using something with a thickness.
    I remember something in the past with trying to bake a navmesh on a mesh/terrain and it wasn't working until I gave it a "thickness", basically getting all the faces on the bottom and just extruding them down by something like 0.1.
    If I didn't do that then it wouldn't bake (If my memory serves me correctly)
     
  7. zmorton36

    zmorton36

    Joined:
    Jan 17, 2019
    Posts:
    1
    If baking a navmesh isn't working then make sure you have every object that you want included in the baking marked as static.