移动版上的Unity Mathf.Clamp问题

时间:2015-10-20 10:42:48

标签: android ios unity3d game-physics physics-engine

我使用此代码:

    void Update () {
    if (SystemInfo.deviceType == DeviceType.Desktop) 
    {
        float xMovement = Input.GetAxis("Horizontal");
        this.rb.velocity = new Vector2(xMovement, 0) * this.speed;


    } else if (SystemInfo.deviceType == DeviceType.Handheld) 
    {
        if (Input.touchCount > 0) 
        {
            Touch touch = Input.GetTouch(0);
            if (touch.position.x > this.westTouchBound && touch.position.x < this.eastTouchBound) 
            {

            } else 
            {
                this.speed = this.ballRigidBody.velocity.magnitude * 2.0f;

                Vector3 location = this.gameCamera.ScreenToWorldPoint(touch.position);

                Vector3 delta = location - this.transform.position;
                Vector2 course = new Vector2(delta.x, 0.0f);
                course.Normalize();
                this.rb.velocity = course * speed;

            }

        } else 
        {
            this.rb.velocity = new Vector2(0, 0);
        }

    }
    float eastMax = this.gameController.screenWidth / 2.0f - this.gameController.wallThickness - this.gameController.barWidth / 2.0f;
    float westMax = -eastMax;

    float actualX = Mathf.Clamp(this.transform.position.x, westMax, eastMax);
    this.transform.position = new Vector3(actualX, this.transform.position.y, this.transform.position.z);

}

在Mac上它运行得很好,但在iOS或Android栏上只是反弹的边界,之后又回来了,它看起来很糟糕。我研究了日志,似乎问题出在物理学上,看起来像PC以某种不同的方式对待它而不是移动。我已经尝试了很多东西,将代码移动到FixedUpdate,LateUpdate,尝试更改控件,甚至尝试分配刚体位置,但它仍然无法正常工作。

1 个答案:

答案 0 :(得分:0)

之前我碰到过这样的事情,

只需替换此

float actualX = Mathf.Clamp(this.transform.position.x, westMax, eastMax);

用这个

if(actualX < westMax)
{
    actualX = westMax;
}
else if(actualX > eastMax)
{
    actualX = eastMax;
}

看起来像夹子坏了什么