2014-06-11 62 views
0

我試圖在Unity 2d中爲我的遊戲實現拖放功能。我在屏幕上有多個相同對象的副本,並且它們只有對撞機名稱不同。我給他們附上了相同的腳本。這是我的一段代碼拖放統一2d

function Start() { 
    playerTouches = [-1, -1]; 
} 

function resetPlayer(touchNumber: int) { 
    for(var i = 0; i < playerTouches.length; ++i) { 
     if(touchNumber == playerTouches[i]) { 
      playerTouches[i] = -1; 
     } 
    } 
} 

function getCollider(vec: Vector2) { 
    var ray : Ray = Camera.main.ScreenPointToRay(vec); 
    var hit : RaycastHit2D = Physics2D.Raycast(ray.origin, ray.direction); 

    if (hit) { 
     if (hit.collider != null) { 
      Debug.Log(hit.collider.name); 
      return hit.collider.name; 
     } else { 
      Debug.Log("is null"); 
      return "null"; 
     } 
    } else { 
     Debug.Log("empty"); 
     return ""; 
    } 
    return ""; 
} 

function processTouch(touch: Touch, touchNumber: int) { 

    if(touch.phase == TouchPhase.Began) { 
     var colliderName: String = getCollider(touch.position); 
     if(colliderName == "Object01" && playerTouches[0] == -1) { 
      playerTouches[0] = touchNumber; 
     } else if(colliderName == "Object02" && playerTouches[1] == -1) { 
      playerTouches[1] = touchNumber; 
     } 
    } else if(touch.phase == TouchPhase.Moved) { 

     // get object and change coords 

    } else if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { 
     resetPlayer(touchNumber); 
    } 
} 

function Update() { 

    if(Input.touchCount > 0) { 
     //Debug.Log("count = " + Input.touchCount); 
     for(var i = 0; i < Input.touchCount; i++) 
     { 
      processTouch(Input.GetTouch(i), i); 
      //Debug.Log("touch : " + i + " " + Input.GetTouch(i).position); 
     } 
    } 
} 

現在我正在檢測用戶觸摸哪個對象。我需要能夠獲得這個對象並改變它的位置。

我也發現這個代碼片斷,它允許移動剛體

var touchDeltaPosition: Vector2 = touch.deltaPosition; 
var touchPosition: Vector2; 
touchPosition.Set(touchDeltaPosition.x, touchDeltaPosition.y); 
rigidbody2D.transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime * spd); 

但不管我選擇什麼對象移動的所有對象。

回答

0

那麼,你可以這樣做。如果您有12份相同的對象並且想移動用戶選擇的對象。所以當用戶觸摸對象時,將GameObject 標籤更名爲另一個標籤。之後,您可以使用一些條件聲明來處理您的代碼。

實施例:

if(Input.GetMouseButtonDown(0)) { 
    Debug.Log("Mouse is down"); 

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    RaycastHit hitInfo = new RaycastHit(); 
    //bool hit = Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo); 
    if(Physics.Raycast(ray, out hitInfo, 30)) { 
     Debug.Log("Hit " + hitInfo.transform.gameObject.name); 
     if(hitInfo.transform.gameObject.tag == "Deselected") { 
      //Debug.Log ("It's working! Attaching MoveCube Script to game :" + hitInfo.transform.gameObject.tag); 

      findObject = GameObject.FindGameObjectsWithTag("Deselected"); 
      foreach(GameObject go in findObject) { 
       //go.gameObject.renderer.material.color = Color.white; 

       go.GetComponent<MoveCube>().enabled = false; 

       if(hitInfo.transform.gameObject.name.Equals(go.gameObject.name)) { 
        //hitInfo.transform.renderer.material.color = Color.white; 
        hitInfo.transform.gameObject.GetComponent<MoveCube>().enabled = true; 
        changeTAG = true; 
       } else { 
        hitInfo.transform.gameObject.tag = "Deselected" 
       } 
      } 

      playerObject = GameObject.FindGameObjectsWithTag("Player"); 
      foreach(GameObject game in playerObject) { 
       count++; 
       if(count == 1) { 
        hitInfo.transform.gameObject.tag = "Player"; 
       } 
       if(count >= 1) { 
        game.gameObject.tag = "Deselected"; 
        game.gameObject.GetComponent<MoveCube>().enabled = false; 
        //game.gameObject.renderer.material.color = Color.white; 
       } 
      } 

      if(changeTAG) { 
       hitInfo.transform.gameObject.tag = "Player"; 
       /*if (hitInfo.transform.gameObject.GetComponent<Rigidbody>()) { 
         Debug.Log ("RigidBody is already added Can't add another RigidBody"); 
         hitInfo.transform.rigidbody.WakeUp(); 

       } else { 
         hitInfo.transform.gameObject.AddComponent<Rigidbody>().useGravity = false; 
         // hitInfo.transform.gameObject.GetComponent<Rigidbody>().WakeUp(); 
       }*/ 

       changeTAG = false; 
      } else if(!changeTAG) { 
       hitInfo.transform.gameObject.tag = "Deselected"; 
      } 

     } else { 
      Debug.Log("Not Working"); 
     } 
    } else { 
     Debug.Log("No hit"); 
    } 
    Debug.Log("Mouse is down"); 
} 

上面的代碼是用於變更爲選擇並取消立方體標記。之後,您可以輕鬆識別所選的遊戲對象,並可將其移動到您想要的位置。

您可以在更新功能中使用此代碼。