2016-07-27 22 views
1

我想這個問題是在路標腳本的某處。玩家跟蹤航點不同取決於速度

如果我讓玩家走慢,讓我們說速度1或2,他走到第一個航點,然後轉向下一個航點,當他到達第二個航點,而不是回到原來的位置,他做了一些圓的步行和然後回到第一個航點。我想要它做的不是走到第一個路點,而是走到它的原始起始位置。

另一件事,如果我改變球員的速度爲7我看到它走得很快,當他到達第二個路標時,他向左轉,繼續向左走。就像取決於球員的速度,他的表現不同。

這是顯示速度2播放器的視頻剪輯:

video clip speed 2

這是:誰走得快是對速度的2看到發生什麼事時,他得到第二航點的第二個玩家 速度第二的球員7

video clip speed 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); 
     } 
    } 
} 

也許這種奇怪的玩家行爲是可以的,因爲物理?所以,如果它太快,它會造成這種行爲?但這很奇怪。我認爲不管它在假期間行走的速度是多少?

回答

1

如果我讓玩家慢慢走讓我們說速度1或2他走到第一個航點,然後轉向下一個航點,當他到達第二個航點,而不是回到原來的位置,他讓一些回合走路然後走回第一個路點。我想要它做的不是走到第一個路點,而是走到它的原始起始位置。

這是因爲當敵人到達最後一個檢查點時,您將currentWaypoint設置爲0.因此,下一幀敵人將尋找該航點。由於你的出發點不是一個航點,他將永遠不會再走到那個地方。你所能做的就是讓另一個與你的敵人開始位置相同的路點。這樣它會回到原來的位置。

另一件事,如果我改變球員的速度爲7我看到它走得快,當他到達第二個路點時他左轉,並繼續走左無止境。就像取決於球員的速度,他的表現不同。

這是因爲移動(位置)和旋轉不與您的巡視速度同步。你只是在增加步行速度而不是旋轉速度。所以每當你的敵人走過一段距離時,都需要重新開始旋轉。嘗試旋轉你的敵人:

transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * patrolSpeed); 

如果這仍然很慢嘗試增加旋轉的speedmodifier。

+0

如何設置字符開始位置的航點?我將腳本添加到每個第三方控制器,所以我將爲每個玩家的開始位置添加另一個兩個航點。但是,在我使用球體的前兩個路標中,有沒有辦法像沒有添加遊戲物體一樣將路點設置爲位置?例如,告訴玩家如何指向0,0,0 –

+0

並在行currentWaypoint = 0;我應該刪除線還是保持這種方式? –

+0

好吧,我添加了兩個新的空對象,並設置在每個字符的開始位置。問題是我改變了line.rotation = Quaternion.Slerp(transform.rotation,rotation,Time.deltaTime * patrolSpeed);這使角色直線移動而不是任何航點。舊的路線讓他們去了航點,但以奇怪的方式,就像我的問題中的視頻剪輯一樣。 –

0

至於第一個問題,你從不存儲「原始」位置 - 你只有路標。因此,當實體到達最後一個航點時,它將爲第一個航點waypoint[0]設置航線,因爲這是LateUpdate()設置爲currentWaypoint

至於基於速度的實體更改行爲,有兩件事要檢查 - 它實際上是否在if(moveDirection.magnitude < 1.5)塊內達到?如果沒有,它在一幀內移動得太快以至於無法檢測。但看起來你總是在移動moveDirection,實際上它不受你計算的旋轉的影響,除非你的Move()函數的工作方式與我預期的不同,所以很可能實體會因此而繼續向一個方向前進。

+0

我如何存儲人物的原始位置,我有兩個字符,並在兩個我添加腳本每個字符的開始位置有點不同第一個球員是在x = -0.066,y = 0,z = 0第二個x = -1.53​​9,y = 0,z = 0那麼我如何在存儲位置的腳本中設置它並將兩個新的檢查點設置爲起點? –

+0

好吧,我會在層次結構中創建兩個新的航點,然後我將每個航點都拖到檢查器中的特定字符作爲腳本中的起始位置,我將更改線路currentWaypoint = 0;到currentWaypoint = 2;是對的嗎 ?我該如何設置層次結構中路標的起始位置? –

+0

如果實體總是需要回到那個(特定)位置,那麼一定要將它添加到Transforms數組中。但是你肯定還是會希望這條線將currentWaypoint重置爲0(否則一旦它爲3,它就會保持重置爲2)。所以,只需讓實體轉換數組的第一個成員而不是最後一個。 –