移動

2015-10-20 30 views
0

團結Mathf.Clamp問題我用這個代碼:移動

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,嘗試更改控件,甚至嘗試分配剛體位置,但它仍然無法工作。

回答

0

我遇到了這樣的事情之前,

只是這個

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

看起來替換此

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

像夾壞了或什麼

+1

這是行不通的。我認爲一個問題在物理學的某個地方。 – starwars25