我正在创建一个 2D 的 Jump & Run 游戏,我想禁用关卡中某个部分的跳跃。我不确定我该怎么做。这是我的跳跃代码:
private void Update()
{
if (Input.GetButtonDown("Jump") && IsGrounded())
{
characterRb.velocity = new Vector2(characterRb.velocity.x, jumpForce);
}
if (Input.GetButtonUp("Jump") && characterRb.velocity.y > 0f)
{
characterRb.velocity = new Vector2(characterRb.velocity.x, characterRb.velocity.y * 0.5f);
}
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
我想我会使用 OnTriggerEnter2D 方法来击中对象,并在更新方法的 if 语句中包含另一个布尔值,但这听起来不太干净:D
我希望有人能帮助我:)
亲切的问候
我想。当角色跳跃并与物体交互时,是否禁用跳跃?不管怎样
添加 bool 和 Collider2D 内容
例如:
public bool canJump = true;
private void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.CompareTag("ground")) {
canJump = false;
}
}
private void OnTriggerExit2D(Collider2D other) {
if (other.gameObject.CompareTag("ground")) {
canJump = true;
}
}
}
你也可以添加
if (Input.GetButtonDown("Jump") && IsGrounded())
替换为这个if (Input.GetButtonDown("Jump") && IsGrounded() && canJump)
如果你愿意,可以看看 Colliders 和 bool stuffs 之类的文档
https://docs.unity3d.com/ScriptReference/Object-operator_Object.html 和 https://docs.unity3d.com/ScriptReference/Collider.html