2024年5月4日发(作者:)

unity 跳跃公式

Unity跳跃公式可以通过以下代码实现:

public class PlayerController : MonoBehaviour {

public float jumpForce = 5.0f; // 跳跃力度

private Rigidbody2D rb; // 刚体组件

private bool isGrounded; // 是否在地面上

void Start() {

rb = GetComponent(); // 获取刚体组件

}

void Update() {

if (Down() && isGrounded) { // 如果按下空格键并且在地面上

ty = new Vector2(ty.x, jumpForce); // 设置垂直方向上的速度为跳跃力度

isGrounded = false; // 离开地面

}

}

void OnCollisionEnter2D(Collision2D other) {

if (eTag("Ground")) { // 如果碰到地面

isGrounded = true; // 在地面上

}

}

}

其中,通过 Down() 监听空格键是否按下,如果按下则给刚体组件添加一个垂直方向上

的速度,即跳跃力度。OnCollisionEnter2D 方法用于检测是否与地面碰撞,如果碰撞到地面,则认为当前处于地面上。