我有一些2D对象,当鼠标悬停时我希望它们消失,函数OnMouseOver()不能很好地工作,所以我尝试获取鼠标位置,然后检查光线投射是否与2D对象发生碰撞。 raycast,但是我不知道为什么它不能很好地工作,有时对象没有反应,有时删除了多个对象
Scanner::next()
答案 0 :(得分:0)
我认为这是因为当您说Destroy(gameObject);您指的是当前的GameObject(脚本附加到的)。另外,应该使用标签if(hit.collider.Comparetag(“ tagname”)
,而不是使用hit.collider.name ==“ item”答案 1 :(得分:0)
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider != null && hit.collider.name == "item")
{
Destroy(hit.transform.gameObject);
}