unity3d 平滑双跳Unity 2D成熟游戏

dkqlctbz  于 2023-03-30  发布在  其他
关注(0)|答案(3)|浏览(147)

我试图使一个顺利的双跳机制为统一2D,平台游戏,但现在的第二跳是更大的,如果你跳后,第一次跳跃,我想有它的其他方式双跳应该扔你更高,如果你按下它后,像在大多数2D平台游戏这是我的代码

private void Update()
       {
            grounded = Physics2D.OverlapArea(transform.position, GroundedEnd.position, 1 << LayerMask.NameToLayer("Ground"));
            if (grounded || !doubleJumpUsed)
            {
                playerRigidBody.AddForce(new Vector2(0, JumpPower));
            }
            if (!grounded && !doubleJumpUsed)
            {
                doubleJumpUsed = true;
            }
            UpdateJumpingAnimator();
        }

        private void UpdateJumpingAnimator()
        {
           if (grounded)
           {
               doubleJumpUsed = false;
           }
        }
iyfamqjs

iyfamqjs1#

请尝试以下代码:

playerRigidBody.AddForce(new Vector2(0, JumpPower), ForceMode2D.Impulse);

在这种情况下,有必要减小变量JumpPower的值

mzmfm0qo

mzmfm0qo2#

老实说,我不知道是否有一个漂亮的方法来做你问.但你有没有尝试捆绑JumpPower到一个计时器?换句话说,越长的字符在空中的时间已经过去了,更多的力量施加到第二跳时,按钮被按下.

raogr8fs

raogr8fs3#

嗨,我尝试过在球体上实现双跳,这是我的代码。看看是否有帮助。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//[RequireComponent(typeof(Rigidbody))]
public class PlayerDoubleJump : MonoBehaviour
{
    public float jumpForce = 5;
    public float groundDistance = 0.5f;

    Rigidbody rigidBody;
    bool canDoubleJump;

    void Awake()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    /*bool IsGrounded()
    {
        return Physics.Raycast(transform.position, Vector3.down, 
groundDistance);
    }*/

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (Physics.Raycast(transform.position, Vector3.down, 
groundDistance))
            {
                rigidBody.velocity = Vector3.up * jumpForce;
                canDoubleJump = true;
            }
            else if (canDoubleJump)
            {
                rigidBody.velocity = Vector3.up * jumpForce;
                canDoubleJump = false;
            }
        }
    }
}

相关问题