![](/img/trans.png)
[英]Why when the camera is child of the character only when pressing on W to move forward the character is not moving and everything is stuttering?
[英]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.