【发布时间】:2014-09-06 03:21:05
【问题描述】:
在 Unity 中寻找处理触摸控制的解决方案后,我发现了一个似乎可行的解决方案。问题是,当检测到触摸时,每个带有 2D 碰撞器的对象都会被破坏。我只希望被触摸的对象被销毁。
场景中的每个游戏对象都是一个预制件。它们都是 8 种不同的随机预制件的克隆。这些预制件中的每一个都有一个 Circle Collider 2D 以及用于触控的脚本 TouchManager.cs
我尝试将“if (hit)”部分更改为“if (hit.collider != null) 但这会导致它由于某种原因无法正常工作。我已经尝试了所有我能想到的方法,但没有任何效果! 有人可以帮忙吗?
TouchManager.cs
// Update is called once per frame
void Update ()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit)
{
touched = true;
startPos = Input.GetTouch(0).position;
}
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (touched == true)
{
float swipeDirection = Mathf.Sign(Input.GetTouch(0).position.y - startPos.y);
if (swipeDirection > 0)
{
Destroy(gameObject);
}
else if (swipeDirection < 0)
{
}
// Reset touched
touched = false;
}
} //END SWITCH
} //END IF TOUCHED
} //END UPDATE
【问题讨论】: