2016-07-05 99 views
0

我一直在嘗試一段時間,並找不出爲什麼它不工作。 我有一個PathHolder父項中的空對象的簡單路徑,它跟隨該路徑並順利旋轉以面對它將要前進的點。我的問題是,有一半的時間,角色對象並沒有一直旋轉,而是仍然朝着應有的方向前進。 另一個奇怪的是,將點移近會減少物體的旋轉量,並且進一步移動它會使角色面對點。但是,在任何情況下工作路徑都需要靈活,所以我不能讓距離影響旋轉。Quaternion.LookRotation角度不能正常工作

理想情況下,我希望角色可以完全順暢地朝向箭頭鍵按下的位置旋轉,以便角色在玩家釋放按鍵時不會卡在遊戲中途。這裏是我的代碼至今:

using UnityEngine; 
using System.Collections; 

public class CharacterControllerPath : MonoBehaviour 
{ 
    enum Direction {Forward, Backward}; 
    Direction charDirection; 

    public PathEditor pathToFollow; 

    private Vector3 targetPosition; 

    public string pathName; 
    public int wayPointID = 0; 
    public float speed; 
    public float rotationSpeed = 7.0f; 

    private float reachDistance = 1.0f; 
    private float distanceX; 
    private float distanceY; 

    void Start() 
    { 
     transform.position = new Vector3(pathToFollow.pathObj[wayPointID].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID].transform.position.z); 
     charDirection = Direction.Forward; 
     Vector3 targetPosition = new Vector3(pathToFollow.pathObj[wayPointID + 1].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID + 1].transform.position.z); 
     transform.LookAt(targetPosition); 
    } 

    void FixedUpdate() 
    { 
     Movement(); 
    } 

    void Movement() 
    { 
     if(Input.GetKey(KeyCode.RightArrow)) 
     { 
      charDirection = Direction.Forward; 

      //Move 
      targetPosition = new Vector3(pathToFollow.pathObj[wayPointID].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID].transform.position.z); 
      transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime); 

      //Rotate 
      targetPosition = new Vector3(pathToFollow.pathObj[wayPointID].transform.position.x, 0, pathToFollow.pathObj[wayPointID].transform.position.z); 
      var rotation = Quaternion.LookRotation(targetPosition); 
      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime); 

      if(transform.position.x == targetPosition.x && transform.position.z == targetPosition.z) 
       wayPointID++; 
     } 
     else if(Input.GetKey(KeyCode.LeftArrow)) 
     { 
      charDirection = Direction.Backward; 

      //Move 
      targetPosition = new Vector3(pathToFollow.pathObj[wayPointID - 1].transform.position.x, transform.position.y, pathToFollow.pathObj[wayPointID - 1].transform.position.z); 
      transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime); 

      //Rotate 
      targetPosition = new Vector3(pathToFollow.pathObj[wayPointID - 1].transform.position.x, 0, pathToFollow.pathObj[wayPointID - 1].transform.position.z); 
      var rotation = Quaternion.LookRotation(targetPosition); 
      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime); 

      if(transform.position.x == targetPosition.x && transform.position.z == targetPosition.z) 
       wayPointID--; 
     } 

     if(wayPointID >= pathToFollow.pathObj.Count) 
     { 
      //Put code to finish the level 
      wayPointID = (pathToFollow.pathObj.Count - 1); 
     } 
     else if(wayPointID <= 0) 
      wayPointID = 0; 
    } 
} 
+0

我假設'pathToFollow'中的點與世界相對?如果你想讓角色朝這個方向看,你需要確保你給出了一個與該角色相關的向量。 – rutter

+0

感謝您的快速回答!但是,我不確定自己完全明白你在說什麼,你有沒有想過的例子? – Septos

+0

因Unity在任何情況下都不會使用四元數。只需使用「LookAt」https://docs.unity3d.com/ScriptReference/Transform.LookAt.html – Fattie

回答

0

如果你想兩個向量之間平滑移動載體,應用

Vector3.Lerp(firstPosOrRotation, secondPosOrRotation, Time.deltaTime*Input.GetInputRaw("Horizontal")*speed); 

Input.GetInputRaw(「水平」)只是之間的值 - 1和1取決於正在按下的方向鍵。 現在只需獲取路徑中的每個點和下一個點以及它們之間的Lerp。

  • 編輯 對於旋轉,代替Time.deltaTime和所有,爲此

    旋轉= Vector3.Lerp(rotationA,rotationB,Mathf.Clamp(DIST(POS, finalPos), 0,1);

    DIST(POS, finalPos)=量在點A和B之間行進

我希望這是清楚的烯ough, 我不確定你的意思,或者你希望對象在路徑上向前。

+0

謝謝你的回答,但正如所提到的,移動工作得很好,我可以從一個點移動到另一個點,問題在於,即使使用我的quaternion.LookRotation(),該角色有時也不會面對正確的方式。我有一條直線,然後是右,然後離開的路徑,然後只需按下右箭頭就可以跟隨它,但是當朝第一個點移動時,角色不會一直旋轉,並且角度爲232.52度角度,然後越過那個點,當它轉向下一個點時,它完全面對它。 – Septos

+0

好的,對於位置做我說的,但對於旋轉做Vector3.Lerp,但替換Time.deltaTime和所有其他東西與對象已在兩個路徑點之間移動多遠,並將其夾在0和1之間 –