-1

Not sure if the StartCoroutine make the stuttering. But when disable the script not to use it in the Inspector the stuttering is gone when using the script it happen.

I'm using a waypoints script attached to ThirdPersonController. I added to the ThirdPersoncontroller also Nav MesH Agent.

I have in the Hierarchy also two Spheres as waypoints in the Naviagtion window i ticked on the checkbox to make them Navigation Static also i did Navigation Static on a Plane.

Then Baked it.

In the Animator window i created new state Walk using HumanoidWalk and set is default so all players start walking automatic when running the game.

The player is walking between the waypoints but it's all stuttering. If i'm not using the script the player is walking fine also other players i have walking fine only when using the script it's stuttering.

This is the waypoints script:

using UnityEngine;
using System.Collections;

public class Patrol : MonoBehaviour
{
    public Transform[] patrolPoints;
    private NavMeshAgent agent;
    private int dest = 0;

    // Use this for initialization
    void Start()
    {
        agent = GetComponent<NavMeshAgent> ();
        StartCoroutine (Patrolling ());
    }


    IEnumerator Patrolling()
    {
        bool startPatorl = false;

        for (int i = 0; i < patrolPoints.Length; i++)
        {
            while (!startPatorl)
            {

                if (agent.remainingDistance < 2.5f)
                {
                    i++;
                    dest = i;
                }

                if (i >= patrolPoints.Length)
                {
                    i = 0;
                    dest = 0;
                }

                agent.destination = patrolPoints[dest].transform.position;
                yield return null;
            }
        }
    }
}
TheLost Lostit
  • 505
  • 6
  • 28
  • Your program is going into infinite loop because `startPatorl` is never set to `true` anywhere in the `Patrolling()` function. The while loop will run while `startPatorl` is `false`. You should set `startPatorl` somewhere. I don't know what you are doing and therefore can't tell where to do this but that is the problem. – Programmer Jul 31 '16 at 20:52
  • @Programmer What i'm doing is moving the player between the waypoints. The script is wrong the way it is (not the startPatrol the whole script logic) ? – TheLost Lostit Jul 31 '16 at 21:05
  • Yeah. Just by looking at it it's wrong. The only reason it did not freeze is because this is a coroutine function and you have `yield return null;` inside the while loop. Without that, it would just freeze you game. Other than that, I don't understand the rest of your code... You have `waypoints` stored in `patrolPoints` and you want to move your player from `patrolPoints[0]` to the length of the `patrolPoints`? – Programmer Jul 31 '16 at 21:12
  • @Programmer You right it's not a good script. I should use and look at the unity docs examples. What i'm trying to do for some time is that the player will walk between waypoints. – TheLost Lostit Jul 31 '16 at 21:15
  • 1
    So the question is: Where are the waypoints stored in the script you have in your question? – Programmer Jul 31 '16 at 21:19
  • @Programmer Indeed this should be the real question. – TheLost Lostit Jul 31 '16 at 21:36

1 Answers1

0

I think the problem is that your coroutine is looping too much , try to change this line

yield return null;

whit this :

yield return new WaitForEndOfFrame();

or this :

yield return new WaitForSeconds(1);
AminSojoudi
  • 1,904
  • 2
  • 18
  • 42