我正在用 Unity 做一个 android 游戏,我不知道该怎么做或多或少地跳跃,这取决于你按下屏幕的时间,我试过了,但我不知道该怎么做,我使用了 deltatime,但它不起作用,至少没有,至少不是我的方式, 所以我想知道该怎么做。角色跳跃,但只是一点点,不管我按下屏幕多少时间,它跳得很低。
这就是我试图实现的方式:
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);
}
}
}
我喜欢在
跳跃时直接使用速度,因为我觉得我可以更精确地使用它,但无论如何,这是我对可变高度跳跃的解决方案。
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;
}
}
这通过改变玩家的速度来工作,如果在达到短跳速度之前抬起手指。如您所见,您必须具有某种接地状态才能正常工作。
我喜欢这种方法,因为它避免了计时器并给了玩家很多控制权。