簡體   English   中英

Unity檢測跌落碰撞

[英]Unity detect collision on drop

我正在嘗試創建一個拖放系統,當我拖動的對象放到其上時,該對象將停留在特定位置,否則將自動返回到原始位置。

public Vector3 dist;
public Vector3 startPos;
public float posX;
public float posY;
public GameObject TopLeftUnfilled;
public GameObject TopRightUnfilled;
public GameObject BottomleftUnfilled;
public GameObject BottomRightUnfilled;
public int count1;
public int count2; 
public int count3;
public int count4;
public bool collided = false;

void Awake()
{
    startPos = gameObject.transform.localPosition;  
}

void OnMouseDown(){
    dist = Camera.main.WorldToScreenPoint(transform.position);
    posX = Input.mousePosition.x - dist.x;
    posY = Input.mousePosition.y - dist.y;

}

void OnMouseDrag(){
    Vector3 curPos = 
        new Vector3(Input.mousePosition.x - posX, 
            Input.mousePosition.y - posY, dist.z);  

    Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
    transform.position = worldPos;
}

void OnMouseUp()
{
    if (collided)
    {
        foreach (Transform childTopLeft in TopLeftUnfilled.transform) 
        {
            count1++;
            if (count1 == 4) 
            {
                gameObject.transform.localPosition = new Vector3 (-2.3f, 1.195f, 0f);
            } 
            else
            {
                gameObject.transform.localPosition = startPos;
            }
        }
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "TileUnfilled") 
    {
        Debug.Log ("collision");
        collided = true;
    }
}

void OnCollisionExit(Collision collision)
{
    collided = false;
}

屏幕截圖

我現在僅使用左上角的未填充區域對此進行測試,但是未檢測到碰撞。 我該怎么辦?

我沒有Unity在我面前進行測試,但我很確定

void OnTriggerEnter(Collider other)

應該

void OnCollisionEnter(Collider other)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM