Unity检测跌落碰撞

时间:2018-07-09 14:58:04

标签: c# unity3d

我正在尝试创建一个拖放系统,当我拖动的对象放在其上时,该对象将停留在特定位置,否则会自动返回到原始位置。

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;
}

Screenshot

我现在仅使用左上角的未填充区域对此进行测试,但未检测到碰撞。我该怎么办?

1 个答案:

答案 0 :(得分:0)

我没有Unity可以测试,但是我很确定

void OnTriggerEnter(Collider other)

应该是

void OnCollisionEnter(Collider other)