Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI Jittering/Not moving - [Issue] #32

Closed
GamesBySaul opened this issue Dec 2, 2020 · 8 comments
Closed

AI Jittering/Not moving - [Issue] #32

GamesBySaul opened this issue Dec 2, 2020 · 8 comments
Assignees
Labels
question Further information is requested

Comments

@GamesBySaul
Copy link

Hi,

Sorry about this, I am getting a weird issue with the asset.
I have a tilemap world, I have baked the navmesh and it's baked nicely, and the AI do move.

However the AI tend to get stuck every now and then and won't move, sometimes they start moving once the player moves or if I just leave it long enough, but I have no idea why this happening.

I have attached some images of:
How the baked navmesh looks
What seems to be occurring

Will also include the code of my AI:
AI can wander around randomly
AI can patrol by moving towards patrol points.

Thanks

Nav Setup

AIGif

Above Gif is not representative of the framerate in-game, everything runs smoothly in-game except for the movement

private IEnumerator PatrolAround()
    {
        while (patrol)
        {
            if (patrolPoints.Count > 0)
            {
                agent.SetDestination(patrolPoints[patrolIndex].position);
                //if (transform.position == patrolPoints[patrolIndex].position || Vector3.Distance(transform.position, patrolPoints[patrolIndex].position) < 0.142f)
                //{
                //    //Debug.Log(Vector3.Distance(transform.position, patrolPoints[patrolIndex].position));
                //    patrolIndex++;
                //}
                yield return new WaitUntil(() => Vector3.Distance(transform.position, patrolPoints[patrolIndex].position) < 0.142f);
                patrolIndex++;
                Debug.Log("Made It");
                if (patrolIndex >= patrolPoints.Count)
                {
                    patrolIndex = 0;
                    
                }
                
                yield return new WaitForSeconds(1f);
               
            }
        }
    }
public IEnumerator WanderAI()
    {
        while (wanderAround)
        {
            Vector3 newPos = RandomNavSphere(transform.position, maxDist, -1);
            agent.SetDestination(newPos);
            if (Vector3.Distance(transform.position, newPos) != Mathf.Infinity) //sometimes the distance is infinity - not sure why
            {
                yield return new WaitUntil(() => Vector3.Distance(transform.position, newPos) < 0.142f);
            }
            yield return new WaitForSeconds(1f);
            //Debug.Log("Woo");
        }
        
    }

public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
  {
    Vector3 ranDir = UnityEngine.Random.insideUnitSphere * dist; //finds a random vector in a sphere distance
        
    ranDir += origin; //adds current pos to that pos
       
    NavMeshHit navhit;
        
    NavMesh.SamplePosition(ranDir, out navhit, dist, layermask); //finds the closest point on navmesh within range, layermask will be -1 as that means all layers
        
    return navhit.position; //return the random position
}
```
@h8man h8man self-assigned this Dec 2, 2020
@h8man h8man added the question Further information is requested label Dec 2, 2020
@h8man
Copy link
Owner

h8man commented Dec 2, 2020

Hello,

NavMeshPlus is making proxies for Unitity's NavNesh System on "bake" and does not affect Agent or any calls to NavMesh.

Make sure that Agent is on NavMesh, that their Z values are equal. Also take a look at demo project for any tips https://github.com/h8man/RedHotSweetPepper

@GamesBySaul
Copy link
Author

Hello,

NavMeshPlus is making proxies for Unitity's NavNesh System on "bake" and does not affect Agent or any calls to NavMesh.

Make sure that Agent is on NavMesh, that their Z values are equal. Also take a look at demo project for any tips https://github.com/h8man/RedHotSweetPepper

Thanks for the quick reply.
I will check the Z values now, but they all appear to be the same value, unless the tilemap has done something (by that I mean I did something wrong).
After further testing two interesting things, I realised:

  1. If the agent that is stuck has another agent walk into it, it will start walking again.
  2. The agent that is stuck its velocity goes to 0 and gets stuck at that, so a little workaround I have found is that I can add a tiny amount to its velocity and round to nearest int and it sorts itself out nicely.

Wondering if those two things change anything? :)

@GamesBySaul
Copy link
Author

Hello,

NavMeshPlus is making proxies for Unitity's NavNesh System on "bake" and does not affect Agent or any calls to NavMesh.

Make sure that Agent is on NavMesh, that their Z values are equal. Also take a look at demo project for any tips https://github.com/h8man/RedHotSweetPepper

Also sorry, I just checked your example scene.
With using a tilemap is there any issue getting the navmesh to check its children and get physics colliders rather than render meshes?

@h8man
Copy link
Owner

h8man commented Dec 2, 2020

@GamesBySaul
Copy link
Author

@GamesBySaul ,

Here is sample for non-tile-mapped scene https://github.com/h8man/RedHotSweetPepper/blob/master/Assets/Scenes/SampleSceneNoTileMap.unity

And here for physics collider
https://github.com/h8man/RedHotSweetPepper/blob/master/Assets/Scenes/SampleSceneCollider2d.unity

Thanks will check them out, I have kinda solved the issue, so I will close this now, thanks again for the help

@MikeSemicolonD
Copy link

I've come across nearly the exact same issue and the solution mentioned here fixed it.

For me it seems to occur when you set a destination, and the NavAgent's X position matches that of the destination's X position. If I call SetDestination multiple times with the same target I get this jittery movement.

Moving the NavAgent a tad to the left/right (by setting it's velocity) gets it moving properly which is very odd.

@GamesBySaul
Copy link
Author

I've come across nearly the exact same issue and the solution mentioned here fixed it.

For me it seems to occur when you set a destination, and the NavAgent's X position matches that of the destination's X position. If I call SetDestination multiple times with the same target I get this jittery movement.

Moving the NavAgent a tad to the left/right (by setting it's velocity) gets it moving properly which is very odd.

Been a while since I touched the project (on hold...indefinitely 😂) but glad it worked! It's little "hacky" things like that which make me love dev (and also hate it)

@MikeSemicolonD
Copy link

MikeSemicolonD commented Jun 24, 2022

I've come across nearly the exact same issue and the solution mentioned here fixed it.
For me it seems to occur when you set a destination, and the NavAgent's X position matches that of the destination's X position. If I call SetDestination multiple times with the same target I get this jittery movement.
Moving the NavAgent a tad to the left/right (by setting it's velocity) gets it moving properly which is very odd.

Been a while since I touched the project (on hold...indefinitely 😂) but glad it worked! It's little "hacky" things like that which make me love dev (and also hate it)

It is what it is. Out of the huge suite of tools & packages Unity and other users offer, there's bound to be one or two things that don't work correctly.

I just wanted to leave a comment explaining the conditions needed to get (and bypass) this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants