我想這個問題是在路標腳本的某處。玩家跟蹤航點不同取決於速度
如果我讓玩家走慢,讓我們說速度1或2,他走到第一個航點,然後轉向下一個航點,當他到達第二個航點,而不是回到原來的位置,他做了一些圓的步行和然後回到第一個航點。我想要它做的不是走到第一個路點,而是走到它的原始起始位置。
另一件事,如果我改變球員的速度爲7我看到它走得很快,當他到達第二個路標時,他向左轉,繼續向左走。就像取決於球員的速度,他的表現不同。
這是顯示速度2播放器的視頻剪輯:
這是:誰走得快是對速度的2看到發生什麼事時,他得到第二航點的第二個玩家 速度第二的球員7
而且這是在C#中的航點腳本
using UnityEngine;
using System.Collections;
public class Waypoints : MonoBehaviour {
public Transform[] waypoint;
public float patrolSpeed;
public bool loop = true;
public int dampingLook = 4;
public float pauseDuration;
private float curTime;
private int currentWaypoint = 0;
public CharacterController character;
// Use this for initialization
void Start() {
}
void LateUpdate(){
if(currentWaypoint < waypoint.Length){
patrol();
}else{
if(loop){
currentWaypoint=0;
}
}
}
void patrol(){
Vector3 nextWayPoint = waypoint[currentWaypoint].position;
// Keep waypoint at character's height
nextWayPoint.y = transform.position.y;
// Get the direction we need to move to
// reach the next waypoint
Vector3 moveDirection = nextWayPoint - transform.position;
if(moveDirection.magnitude < 1.5){
Debug.Log("enemy is close to nextwaypoint");
// This section of code is called only whenever the enemy
// is very close to the new waypoint
// so it is called once after 4-5 seconds.
if (curTime == 0)
// Pause over the Waypoint
curTime = Time.time;
if ((Time.time - curTime) >= pauseDuration){
Debug.Log("increasing waypoint");
currentWaypoint++;
curTime = 0;
}
}
else
{
Debug.Log("reaching in rotation " + moveDirection.magnitude);
// This code gets called every time update is called
// while the enemy if moving from point 1 to point 2.
// so it gets called 100's of times in a few seconds
// Now we need to do two things
// 1) Start rotating in the desired direction
// 2) Start moving in the desired direction
// 1) Let' calculate rotation need to look at waypoint
// by simply comparing the desired waypoint & current transform
var rotation = Quaternion.LookRotation(nextWayPoint - transform.position);
// A slerp function allow us to slowly start rotating
// towards our next waypoint
transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
Time.deltaTime * dampingLook);
// 2) Now also let's start moving towards our waypoint
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
}
也許這種奇怪的玩家行爲是可以的,因爲物理?所以,如果它太快,它會造成這種行爲?但這很奇怪。我認爲不管它在假期間行走的速度是多少?
如何設置字符開始位置的航點?我將腳本添加到每個第三方控制器,所以我將爲每個玩家的開始位置添加另一個兩個航點。但是,在我使用球體的前兩個路標中,有沒有辦法像沒有添加遊戲物體一樣將路點設置爲位置?例如,告訴玩家如何指向0,0,0 –
並在行currentWaypoint = 0;我應該刪除線還是保持這種方式? –
好吧,我添加了兩個新的空對象,並設置在每個字符的開始位置。問題是我改變了line.rotation = Quaternion.Slerp(transform.rotation,rotation,Time.deltaTime * patrolSpeed);這使角色直線移動而不是任何航點。舊的路線讓他們去了航點,但以奇怪的方式,就像我的問題中的視頻剪輯一樣。 –