Random Wandering with Navagent

I’m trying to get my characters to randomly wander with a navmesh, I have a working move script for point and click moving, so I thought I could just implement that, and make the characters wander in a vector3 with random.Range. It kinda worked, here is my original movement,

        if (isWalking && !bimbus.isDead)
        {
                Vector3 dest = new Vector3(Random.Range(-5, 5),0,Random.Range(-5,5));
                move.MoveUnit(transform.position + dest);

        }

but there was times where it would make weird jerky movements when the distance was too small, so I changed it to this to try and prevent that,

        if (isWalking && !bimbus.isDead)
        {
            Vector3 dest = new Vector3(Random.Range(-5, 5),0,Random.Range(-5,5));
            if (Vector3.Distance(transform.position, dest) > 2 || Vector3.Distance(transform.position, dest) < -2)
            {
                move.MoveUnit(transform.position + dest);
            }
            else
            {
                dest = new Vector3(Random.Range(-5, 5), 0, Random.Range(-5, 5));
            }
            isWalking = false;
        }

But now it’s even worse haha. They still move in very short distances, I’d thought 2 would be far enough… But did I make a mistake in the code or something?

Thanks in advance

Once again, use lots of Debug.Log() statements. This is ALWAYS the best approach for interactive debugging.

I theorize that some other code is making isWalking = true, even after you set it to false, so it is rechoosing destinations every frame.

1 Like

Well, the issue isn’t that it’s constantly moving, it’s just sometimes the range is too short, and it causes it to walk like 10 centimeters and the animation looks weird and jittery lol. It wanders fine, provided the distance is far enough from the origin.

And trust me, I use TONS of print(“Is this working?”) s lol

Hi, I just quickly looked at your code. Your distance check makes no sense. You test if a value is greater than x and then also if value is smaller than x. Why? You just want to know if distance is under or over a threshold you have defined (like the 2 units you have there). Distance is same as vector A - B magnitude, in other words distance between two points.

1 Like

I figured it was a little messed lol, but it’s checking if the distance is greater than 2 and less that -2, because the random.Range is from -5 to 5. So should I be checking magnitude?

A distance is a magnitude and therefore cannot be less than 0. Imagine a line segment on a piece of paper. Is the length of the line segment ever less than 0, no matter where you put it on the paper?

1 Like

Ahh, I’m dumb. I was thinking because I had set it from -5 to 5, but forgot I was checking distance separately lol. Well, I removed the < -2 and created serialized variables for wander range and minimum wander range, set it to 3 minimum, and 10 max. I wasn’t thinking, it really was just that 2-5 was a really short distance to wander, and because the range was so small it was often ending up in the 2-3 range.

Thanks for the help guys.