【问题标题】:touch phases behavior issue触摸阶段行为问题
【发布时间】:2015-02-06 20:08:52
【问题描述】:

我正在 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”,我希望它会是这样。然而,结果,我按下它,它的比例改变了,但只有一次。我再次按下,没有任何反应。我该怎么办?

【问题讨论】:

    标签: android input unity3d touch


    【解决方案1】:

    这个问题与触摸阶段的行为无关,你很好地实现了它。

    问题是您每次触摸游戏对象时都将其设置为相同的比例。为了让你的游戏对象好起来,你必须像这样设置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);
    

    【讨论】:

    • 是的,就是这样。我的意思是白色的“hit.transform”,而不仅仅是“transform”。但我没有。非常感谢您指出这个愚蠢的错误!
    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2015-08-02
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多