[英]touch phases behavior issue
我正在Unity3D中为触摸屏设备制作2D游戏,而我正试图制作类似“GetMouseButtonDown”的东西。 这是我的代码:
if (Input.touchCount > 0) {
foreach (Touch touch in Input.touches) {
Vector3 i = Camera.main.ScreenToWorldPoint (touch.position);
RaycastHit2D hit = Physics2D.Raycast (i, i);
if (hit.transform != null) {
string tag = hit.transform.gameObject.tag;
if (touch.phase == TouchPhase.Began) {
hit.transform.localScale = new Vector2(-transform.localScale.x, -transform.localScale.y);
}
}
}
}
我希望被激活的对象改变比例。 而且我希望它能像我使用“GetMouseButtonDown”一样。 然而,结果,我按下它,它的比例变化,但只有一次。 我再次按下,没有任何反应。 我该怎么办?
问题与触摸阶段行为无关,您正在很好地实现它。
问题是,每次触摸时,您都将游戏对象设置为相同的比例。 为了你的游戏对象你必须像这样设置hit.transform.localScale
:
hit.transform.localScale = new Vector2(-hit.transform.localScale.x, -hit.transform.localScale.y)
请注意,每次触摸游戏对象时,都要将x和y缩放设置为transform.localScale
,这是一个常量值。
另请注意,使用此句子,您只需在每次触摸游戏对象时轻扫刻度。 如果你想迭代地扩展你的游戏对象,使它变小,你必须做这样的事情:
float scaleFactor = -0.2f;
hit.transform.localScale += new Vector2(hit.transform.localScale.x*scaleFactor, hit.transform.localScale.y*scaleFactor);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.