简体   繁体   中英

In unity, Why my character is moving so strange and everything stuttering when using way points?

First a shot video showing how the character is moving. What i want to do is to make the character to walk not just to move but to walk from one or more given point/s to another point/s.

Moving Character Video

And now what i did so far.

Added to my Hierarchy: Terrain, Main Camera, Directional Light, Cylinder and ThirdPersonController.

In the ThirdPersonController i clicked on the menu: Component > Navigation > Nav Mesh Agent

Then dragged to the ThirdPersonController a Patroll.cs script.

In the ThirdPersonController Inspector in the Patroll area i added 2 points Walls and Cylinder. I want the character to walk from the Walls(building) to the Cylinder. Also in the Patroll area i added to the Agent: ThirdPersonController (Nav Mesh Agent).

Then in the Jierarchy i clicked on the Terrain clicked ot make it static then clicked in the menu on Window > Navigation > Bake and then clicked on the Bake button.

Then when running the game the character is starting moving automatic in strange way and everything is stuttering. Before added the Patroll script and the Nav Mesh Agent to the ThirdPersonController i could move the character smooth using WSAD keys.

The goal for now what i want to do is once running the game if i walk using WSAD keys close or touch the walls(building) then the character will start walking automatic to the cylinder position. Or when running the game the character will start walking automatic from it's starting position to the walls and then to the cylinder 3 way points.

This is the Patroll script in c#

using UnityEngine;
using System.Collections;

public class Patroll : MonoBehaviour {

    public Transform[] points;
    private int destPoint = 0;
    public NavMeshAgent agent;

    // Use this for initialization
    void Start () {


        agent = GetComponent<NavMeshAgent>();
        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        GotoNextPoint();

    }

    void GotoNextPoint() {
        // Returns if no points have been set up
        if (points.Length == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }


    void Update () {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (agent.remainingDistance < 0.5f)
            GotoNextPoint();
    }
}

ThirdPersonController脚本组件包含ThirdPersonController脚本组件时, Patroll它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM