2014-09-22 28 views
0

我無法創建一種方法來檢查播放器是否與某個「航點」發生碰撞,如果我單擊該航點。我已經設置了玩家希望能夠移動到的7個路點。驗證單擊後是否與某個對象發生碰撞

現在我正在嘗試編寫一段腳本,檢查點擊後的路點(onMouseDown)是否與玩家發生碰撞。因爲如果是這種情況,它不會計算移動到的位置。

public class WayPointPositioner : MonoBehaviour { 

private Vector3 wayPointPosition; 
public GameObject playercheck; 

//Check if object is clicked 
void OnMouseDown() 
{ 
    Debug.Log ("Object Clicked " + GameObject.name); 

    // Check if collision occurs with playercheck 

    OnCollisionEnter(playercheck != Collision) 
    { 
     // If its the player, then return a new position for the player to move to for walking 
     // Else debug that its not so 
     if (playercheck.gameObject.CompareTag("Player")) 
     { 

      Debug.Log ("Object not colliding and retrieving position"); 

      wayPointPosition = new Vector3 (GameObject.X, GameObject.Y, 10); 
      wayPointPosition = Camera.main.ScreenToWorldPoint(wayPointPosition); 

     } 
     else 
     { 

      Debug.Log ("Object is colliding, no movement needed"); 

     } 
    } 
} 

} 

現在我已經知道OnCollisionEnter將無法​​正常工作。因爲它需要在它之前運行的void-statement。但我不知道我能做到這一點。

回答

0

管理修復它。只是一個白癡。

關鍵是要同時使用這兩個功能。因此,我將檢查Mousedown的任務分成幾部分,並通過創建一個單獨的變量來檢查這一切是否被佔用。

public class WayPointPositioner : MonoBehaviour { 

private Vector3 wayPointPosition; 
private bool checkPlayerWaypointCollision; 

void Start() 
{ 
    wayPointPosition = transform.position; 
} 

void OnTriggerStay2D (Collider2D other) 
{ 
    if (other.gameObject.name == "Player") 
    { 
     checkPlayerWaypointCollision = true; 
    } 
    else 
    { 
     checkPlayerWaypointCollision = false; 
    } 

} 

//Check if object is clicked 
void OnMouseDown() 
{ 

    // If its the player, then return a new position for the player to move to for walking 
    // Else debug that its not so 
     if (checkPlayerWaypointCollision == false) 
     { 

      Debug.Log ("Object not colliding and retrieving position"); 

      transform.position = new Vector3 (transform.position.x, transform.position.y, 10); 
      wayPointPosition = Camera.main.ScreenToWorldPoint(wayPointPosition); 

     } 
     else 
     { 

      Debug.Log ("Object is colliding, no movement needed"); 

     } 

} 

} 
0

我只是比較玩家的位置和方式點。如果相等,那麼玩家在那裏移動到路點。希望我理解這個問題是對的。

+0

不,航點系統在那裏,所以玩家角色最終會沿着航點移動到航點的目的地。示例:他從Waypoint 02開始,需要前往Waypoint 06,然後在03-04-05之間前進至06。 – Veraduxxz 2014-09-22 12:00:24