繁体   English   中英

在 unity 中,为什么我的角色在使用航点时移动如此奇怪并且一切都口吃?

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

首先是一个显示角色如何移动的拍摄视频。 我想要做的是让角色走路不仅仅是为了移动,而是从一个或多个给定的点走到另一个点。

移动角色视频

现在我到目前为止所做的。

添加到我的层次结构中:地形、主相机、定向光、圆柱体和第三人称控制器。

在 ThirdPersonController 中,我单击菜单:Component > Navigation > Nav Mesh Agent

然后将一个 Patroll.cs 脚本拖到 ThirdPersonController 中。

在巡逻区域的 ThirdPersonController Inspector 中,我添加了 2 点 Walls 和 Cylinder。 我希望角色从墙壁(建筑物)走到圆柱体。 同样在巡逻区域我添加到代理:ThirdPersonController(导航网格代理)。

然后在 Jierarchy 中,我单击 Terrain 单击 ot 使其静态,然后单击 Window > Navigation > Bake 菜单,然后单击 Bake 按钮。

然后在运行游戏时,角色开始以奇怪的方式自动移动,一切都在口吃。 在将巡逻脚本和导航网格代理添加到 ThirdPersonController 之前,我可以使用 WSAD 键平滑移动角色。

现在我想要做的目标是一旦运行游戏,如果我使用 WSAD 键关闭或触摸墙壁(建筑物),那么角色将开始自动行走到圆柱体位置。 或者在运行游戏时,角色将开始自动从起始位置步行到墙壁,然后再到圆柱体的 3 个路径点。

这是 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它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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