在Unity中击中物体时如何禁用跳跃?



我正在创建一个跳转&在2D中运行游戏,我想在关卡的某个部分禁用跳跃。我不知道该怎么做。这是我的跳转代码:

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

我希望有人能帮我一下:)

亲切的问候

我想。当角色跳跃并与物体交互时,是否禁用跳跃?不管怎样

add bool and Collider2D stuff

例如:

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之类的东西的文档

https://docs.unity3d.com/ScriptReference/Object-operator_Object.html和https://docs.unity3d.com/ScriptReference/Collider.html

相关内容

  • 没有找到相关文章

最新更新