Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Anyone know of any good navmeshagent tutorials that use a radius?

Discussion in 'Scripting' started by Lethn, Jul 29, 2015.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I can find tons of tutorials detailing how to get an NPC in unity to go from one point to another but anything else seems to involve waypoints. I'd like to create some truly random movement where the AI just wonders about within a specific radius and I'm searching around right now for tutorials but nothing really seems to be coming up on google.
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    If you just need the point to which he should go try ->

    Code (CSharp):
    1. Vector3 newPosition = oldPosition + (Vector3)(Random.insideUnitCircle * radius);
    if he should walk around a fix posiotion the fix position should remain your oldPosition.
     
    Lethn and LeftyRighty like this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    might not be that exact syntax, according to the documents Random.insideUnitCircle sets the x and y values of a vector3, in a top down 3d world you'll want x and z with constant y...
     
    Lethn likes this.
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Yea true, i'm using it for spawning drop in a 2D Game :)
     
    Lethn and LeftyRighty like this.
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Ohh thanks! I should be able to do something with that, sometimes it seems to be just finding the right keywords too. I do try not to spam with questions but sometimes it's far easier asking than spending hours searching on google.
     
    Last edited: Jul 29, 2015
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Just thought I'd say thanks to martinmr mentioning the right code I found a thread on the exact same subject that I was asking about.

    http://forum.unity3d.com/threads/ai-random-wandering.177867/

    lol! It really is all in the keywords, when I tried to put just radius and wondering in nothing but junk came up.

    Edit: Also found this.
     
    Last edited: Jul 29, 2015
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Hmm could I have some help with the full syntax? I get the general idea but assigning these variables is annoying me, this is why I wanted a tutorial.

    What variable types etc. exactly are lastPosition for example? Radius is pretty self-explanatory.

    WOH! F***! I did it but it came out very weird LOL! Used the unity tutorial to do it, I think I need a bigger radius. Okay, I've assigned it to my navmeshagent capsule and it's definitely transforming to random points within the sphere.

    However, it's doing it ridiculously fast and is just generally very glitchy, here's what I have so far.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VisitorAI : MonoBehaviour {
    5.  
    6.     NavMeshAgent agent;
    7.  
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.         agent = GetComponent<NavMeshAgent> ();
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.         agent.SetDestination (transform.position = Random.insideUnitSphere * 5);
    21.  
    22.  
    23.     }
    24. }
    25.  
    I think this is to do with me using transform rather than target? I don't know..... Oop! Sorry, my brain has suddenly engaged now and I ended up almost fixing it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VisitorAI : MonoBehaviour {
    5.  
    6.     NavMeshAgent agent;
    7.  
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.         agent = GetComponent<NavMeshAgent> ();
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.         agent.SetDestination (Random.insideUnitCircle * 5);
    21.  
    22.    
    23.     }
    24. }
    25.  
    The movement is a lot smoother now and cleaner, but it's still quite weird.
     
    Last edited: Jul 29, 2015
  8. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Right, I think I understand what's happening now after looking at my little capsule agent riggle around, what I need is a delay on the next time it makes a calculation to change direction.

    Do you guys know of a small algrorithm I can use to delay the random.insideUnitSphere/insideUnitCircle or it is as simple as putting in a wait command?

    Current code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VisitorAI : MonoBehaviour {
    5.  
    6.     NavMeshAgent agent;
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.         agent = GetComponent<NavMeshAgent> ();
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.         agent.SetDestination (Random.insideUnitSphere * 50);
    20.    
    21.     }
    22. }
    23.  
     
    Last edited: Jul 29, 2015
  9. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I also have another question in regard to Navmesh Agent, why would the agent successfuly detect my static objects but not detect static object prefabs and just walk right through them if they're being pushed around a bit? Tweaking the settings and playing around but they really don't like it when they're being forced to navigate newly created obstacles while the program is running.
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have the target as a vector that is updated. To keep him within the original radius set the vector for the original position & never update it,
    Set the target selection within an if statement. When the player is <=0.01f of the target pick a new target.

    We're the prefabs in place when the na mesh was baked?
     
  11. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    No I just had them in the assets folder, that might explain why it's not updating itself, surely there's a better way? The reason being is I'm thinking of turning this into a city builder/management game and it's going to look odd having prefabs tucked away in the corner of the map for the sake of keeping things functional, they spawn when you click buttons.

    Will have a look at the stuff you mentioned and experiment thanks for the comments, aside from what I mentioned everything is working fine now and I can tweak stuff without issues.
     
  12. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Did you have one of the prefabs in the screen when you bake it, then you should be able to remove it from the scene & run.
     
  13. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    That's what the "NavMesh Obstacle" scripts are for. Just enable carving if you want it to avoid it in the pathfinding process, rather than just moving around it as best it can while it's moving.
     
  14. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Ah, hadn't realised that's what carve does as I'm still new to Unity, I'll go and test all this out.

    Thank you! The capsules now seem to be nearly perfectly avoiding it, I expect the slight clipping is due to the closeness of the actual navmesh obstacle collider.

    Edit: oop damn spoke too soon, sometimes they still prefer to go through it on occasion, they avoid it much more now though, might be just a matter of tweaking settings.

    Same results trying what you suggested I'm afraid, they still like to clip through the prefab cubes I'm placing while in the game, they do try to avoid it, but after awhile they just seem to not bother and go straight through.

    I should clearly point out I'm spawning cubes in to try and make the capsules react, this is to simulate as if I was placing a building in a city builder. Better to do all of this with placeholders and so on than waste time making detailed models on something I don't know works properly.
     
    Last edited: Jul 30, 2015
  15. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Just a note! I figured out the clipping issues, it looks like it was just the agent height and agent size settings, for those that don't know anything you want to spawn in as an obstacle has to have higher numbers than the AI Agent you have moving. Hope this thread saves people the trouble of having to look for along time if they're struggling with getting the whole Navmesh system to work properly.
     
    tedthebug likes this.
  16. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I just had a question though that's very specific to game mechanics after thinking about it some time. When I add more cubes to start hassling the AI into a corner and get them to adapt, I've noticed it actually starts taking longer and longer to re-caclulate the next path to take.

    Is this some kind of limitation to do with the AI or is there a tweak I can make in code somewhere to shorten the delay time, or is this something more basic like my computer itself having to do lots of calculations? I don't think it can be, because I've put swarms of AI in with just the basic obstacles and they were all wondering around absolutely fine.

    EDIT: Just so you know, I found a solution to the pausing NPCs, it wasn't anything to do with calculating what had happened It was I had set the radius of the sphere ( which the NPCs wonder in ) far too high and the NPCs were simply trying to go somewhere they couldn't.

    If you experiment and try to match the radius with your actual landscape or terrain everything should work fine.
     
    Last edited: Aug 5, 2015