2016-07-15 13 views
0

我正在做一個與Unity的android遊戲,我不知道如何做或多或少跳轉,取決於你按了多長時間的屏幕,我試過了,但我不知道該怎麼做,我使用deltatime,但它不起作用,至少沒有,至少不以我做的方式,所以我想知道如何做到這一點。角色跳躍,但只是一點點,不管我按了多少時間屏幕,它跳得這麼低。如何根據您在Android(c#)中按多長時間按屏幕來更多或更少地跳轉?

這是怎麼了,我想實現這一目標:

void Update() {  
     movimiento(); 

     if (transform.position.x <= 4.65f) { 
      SceneManager.LoadScene ("Game Over"); 
     } 
     if (Input.touchCount > 0) { 
      GetComponent<Animator>().Play ("Andar 2"); 
      print (Input.GetTouch (0).deltaTime); 
      if (Input.GetTouch (0).deltaTime >= 2) { 
       GetComponent<Rigidbody2D>().AddForce (Vector3.up * Time.deltaTime * 20000); 
       GetComponent<Animator>().Play ("Andar 2"); 
      } else if (Input.GetTouch (0).deltaTime >= 1) { 
       GetComponent<Rigidbody2D>().AddForce (Vector3.up * Time.deltaTime * 2000); 
      } else if (Input.GetTouch (0).deltaTime < 1) { 
       GetComponent<Rigidbody2D>().AddForce (Vector3.up * Time.deltaTime * 200);  
      } 
     } 
    } 

回答

0

我喜歡速度的工作做直接跳轉時,我覺得我可以用它更精確,但無論如何,這是我的可變高度跳躍的解決方案。

void Update() 
{ 
    // set jump controls 
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
     jump = true; 

    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && !grounded) 
     jumpCancel = true; 
} 

void FixedUpdate() 
{ 
    // if player presses jump 
    if (jump) 
    { 
     rigidbody.velocity = new Vector3(rigidbody.velocity.x, jumpVelocity, rigidbody.velocity.z); 
     jump = false; 
    } 

    // if player performes a jump cancel 
    if (jumpCancel) 
    { 
     if (rigidbody.velocity.y > shortJumpVelocity) 
      rigidbody.velocity = new Vector3(rigidbody.velocity.x, shortJumpVelocity, rigidbody.velocity.z); 
     controller.jumpCancel = false; 
    } 
} 

這是通過改變玩家的速度如果達到shortJumpVelocity之前的手指擡起。正如你所看到的,你必須有一個基礎的狀態才能正常工作。

我喜歡這種方法,因爲它避免了定時器,並給玩家很多控制權。

相關問題