AI Wandering Issue

First of all, here’s my code:

if (wanderMovement)
            {
                Vector2 randCircleLoc = Random.insideUnitCircle * 20;

                if (wanderTime < Random.Range(20,60))
                {
                    inputMovement.x = randCircleLoc.x - transform.position.x;
                    inputMovement.z = randCircleLoc.y - transform.position.z;
                    inputRotation = inputMovement;
                    wanderTime += Time.deltaTime;
                }
                else
                {
                    waitTime += Time.deltaTime;
                    if(waitTime < Random.Range(2,30))
                    {
                        inputMovement = Vector3.zero;
                    }
                    else
                    {
                        wanderTime = 0;
                        waitTime = 0;
                    }
                    
                }
            }

I’m just trying to get my enemies to pick a random spot inside a 20 radius circle and move towards it (eventually getting them to pick a new spot once they arrive, for infinite wandering).

The problem is, when I start this up, they just do this weird twitching where they are trying to pick a bunch of different rotations and it looks like they are just spazzing out. On top of that, they will only head towards the center of the map, to what I think is 0,0,0.

I have no idea why they would be always be picking 0,0,0 as their travel to spot when it’s supposed to be random and I have no idea why they are twitching. I took out all of my AI code except that and they were still doing it, so it’s not an issue with the rest of my code.

Thanks for the help. Boy do I miss the simplicity of XNA after using Unity so much haha.

Ok I ended up figuring out how to fix the twitching and movement, kind of. I guess it was trying to set a new randcircleloc every time it went through the script, so it was tweaking it. When I put in something for it to only happen once, they stopped twitching and started going in random directions.

New problems are that the movement always seems to be in the same general direction. I’m rarely getting them to go between 0 and 180 degrees, but they will easily go between 180-360 degrees. Might just be weird coincidence and I’m sure it won’t be noticeable in the long run (as they will slowly turn the other way theoretically).

Last problem is that when they stop, they twitch up and down in the Z direction. Still working on that…

figured it out again.

The enemies were reaching their locations and still trying to move in some direction until their time to stop wandering was up, which was causing them to move passed it, turn around, move passed it, turn around, rinse and repeat. Woops.

I hope that someone finds this code useful in the future at least lol.