【问题标题】:Why when pressing on the space key to jump if the player is in the air and pressing on space again he jump once again higher?为什么当玩家在空中按下空格键跳跃时,再次按下空格键会再次跳得更高?
【发布时间】:2021-09-27 21:25:43
【问题描述】:
public bool useCharacterForward = false;
        public bool lockToCameraForward = false;
        public float turnSpeed = 10f;
        public KeyCode sprintJoystick = KeyCode.JoystickButton2;
        public KeyCode sprintKeyboard = KeyCode.Space;
        public bool useJump = false;
        public Vector3 jump;
        public float jumpImpulse;

        private float turnSpeedMultiplier;
        private float speed = 0f;
        private float direction = 0f;
        private bool isSprinting = false;
        private Animator anim;
        private Vector3 targetDirection;
        private Vector2 input;
        private Quaternion freeRotation;
        private Camera mainCamera;
        private float velocity;
        private bool isGrounded;
        private Rigidbody rb;

        // Use this for initialization
        void Start()
        {
            anim = GetComponent<Animator>();
            mainCamera = Camera.main;
            rb = GetComponent<Rigidbody>();
            jump = new Vector3(0.0f, 2.0f, 0.0f);
        }

        private void OnCollisionStay(Collision collision)
        {
            if (collision.gameObject.tag == "Ground")
            {
                isGrounded = true;
            }
        }

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space) && isGrounded && useJump)
            {
                rb.AddForce(jump * jumpImpulse, ForceMode.Impulse);
                isGrounded = false;
            }
        }

即使玩家在空中并且即使我正在检查地面标签,我仍然可以在玩家跳跃后在空中时再次按下空间,它会跳跃并移动玩家在空中更高。

但我将 isGround 设置为 false 并且玩家离地面更高,并且仍然按下空间使其在空中跳跃时已经在空中。

此屏幕截图显示了地面对象是如何在层次结构中构建的。 玩家有一个胶囊对撞机和一个刚体。

有 3 个楼层对象:

Floor example

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    物理(刚体)仅在FixedUpdate 中更新。此外,OnCollisionStay 在每个对撞机的 FixedUpdate 循环内被调用。

    在您的设置中,有一点机会在您第一次按下后再次触发OnCollisionStay,并将isGrounded 再次设置为true,让您稍后再跳一次。

    我认为如果您将跳转移动到FixedUpdate 甚至OnCollisonStay 本身,它已经可以防止这种情况:

    private bool isJump;
    private bool isGrounded;
    
    private void OnCollisionStay(Collision collision)
    {
        // In general rather use CompareTag instead of ==
        // It causes an error If the tag is non-existent or has a typo making 
        // your debugging way easier and is also slightly more efficient
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }
    
    private FixedUdpate()
    {
        // Should we jump?
        if(isJump && isGrounded)
        {
            rb.AddForce(jump * jumpImpulse, ForceMode.Impulse); 
            isGrounded = false;
        }
    
        isJump = false;
    }
    
    private void Update()
    {
        // As you already did correctly get single event user input in Update
        // in order to not miss one
        // I would check the cheaper bool flag first
        if (isGrounded && useJump && Input.GetKeyDown(KeyCode.Space))
        {             
            isJump = true;
        }
    }
    

    现在应该确保只能在与地面碰撞时应用跳跃

    【讨论】:

    • 这个解决方案解决了这个问题,但现在如果我连续两次快速按下空格键,玩家将跳跃一次,然后在落地时他会再次自动跳跃。就像它在记忆中记住第二次空格键按下并进行第二次跳跃。
    • 如果我使用另一个 bool 助手,当连续按两次空格键时,它将避免在着陆时第二次跳跃。但是我在哪里再次将 helper bool 设置为 false ? private void Update() { // 正如您已经在 Update 中正确获取单个事件用户输入 // 为了不错过一个 // 我会先检查更便宜的 bool 标志 if (useJump && Input.GetKeyDown(KeyCode.Space) && jumpOnce == false) { isJump = true;跳转一次=真; } }
    • @ChongCraig 查看更新后的答案...我认为如果您将其正确拆分为碰撞并修复更新并简单地重置isJump,那么空中 aou 也不会再有这个问题了跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多