Unity 3D:如何停止跳伞 [英] Unity 3D: How to stop being able to jump in the air

查看:99
本文介绍了Unity 3D:如何停止跳伞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在3D世界中实现在2D精灵上跳跃. 我可以跳得很好,但是我可以一直跳下去,这是我不想要的. 我已经尝试了好几个小时才能找到问题,但仍然无法提出解决方案.

I am trying to achieve jumping on a 2d sprite in 3d world. I can jump fine but I'm able to keep jumping in the air which I do not want. I've been trying to find the problem for hours but still cannot come up with a solution.

{ 
   public float jumpHeight = 5f;
    public static bool isJumping = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && (isJumping == false))
        {
            gameObject.GetComponent<Rigidbody>().AddForce(new Vector2(0f, jumpHeight), ForceMode.Impulse);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "Ground")
        {
            isJumping = false;
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.collider.tag == "Ground")
        {
            isJumping = true;
        }
    }
}

我想念什么吗?

推荐答案

将碰撞器作为触发器放置在2D对象上,并检查其是否与地形或地面碰撞.只有那样你才能跳.与地面碰撞的射线也可以这样做.您不需要使用两者,只需跳OnCollisionStay就可以了

Put a collider as trigger on your 2D object and check if it is colliding with the terrain or ground. Only then you can jump. The same could be done with a Ray which collides with the ground. You dont need to use both just jump OnCollisionStay and thats it

这篇关于Unity 3D:如何停止跳伞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆