繁体   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