Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按键冷却_C#_Unity3d - Fatal编程技术网

C# 按键冷却

C# 按键冷却,c#,unity3d,C#,Unity3d,我正在做一个单独的项目,即水平2D无限长跑。我在跳跃力学方面遇到了一个问题,玩家可以按住跳跃按钮,一碰到地面就跳。我想强迫玩家释放按钮,以便再次跳跃。我想在他漂浮的时候做同样的动作(在跳跃结束时,当球员开始下降时,其y速度会降低几秒钟)。我遵循单一责任原则,所以跳跃和浮动是两个独立的脚本 我曾尝试实现一个计时器,当玩家触地时计时,一段时间后玩家将能够再次跳跃,但没有得到询问的结果,因为你可以一直按住跳跃按钮,在确定了在地面上花费的时间后,玩家只会再次跳跃而不释放按钮 public class J

我正在做一个单独的项目,即水平2D无限长跑。我在跳跃力学方面遇到了一个问题,玩家可以按住跳跃按钮,一碰到地面就跳。我想强迫玩家释放按钮,以便再次跳跃。我想在他漂浮的时候做同样的动作(在跳跃结束时,当球员开始下降时,其y速度会降低几秒钟)。我遵循单一责任原则,所以跳跃和浮动是两个独立的脚本

我曾尝试实现一个计时器,当玩家触地时计时,一段时间后玩家将能够再次跳跃,但没有得到询问的结果,因为你可以一直按住跳跃按钮,在确定了在地面上花费的时间后,玩家只会再次跳跃而不释放按钮

public class Jumping : MonoBehaviour
{
    public bool isJumping;
    public bool isGrounded;
    public float speedUp;
    public float verticalAxis;

    public float timePassed;
    public float jumpLimit;
    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        verticalAxis = Input.GetAxisRaw("Vertical");
        if (verticalAxis > 0 && timePassed == 0)
        {
            isJumping = true;
        }
    }

    private void FixedUpdate()
    {
        if (isJumping)
        {
            Jump();
        }
    }

    private void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {

            isGrounded = true;
            timePassed = 0f;
        }
    }


    private void Jump()
    {
        isGrounded = false;
        //Modifying y component of players rigidbody(jumping)
        Vector2 velocity = Vector2.up * speedUp * verticalAxis;
        rb.velocity = velocity;

        //Counting time when jumping 
        timePassed += Time.deltaTime;
        if (timePassed >= jumpLimit)
        {
            isJumping = false;
        }
    }
}
公共课堂跳跃:单一行为
{
公共场所在跳跃;
公共学校停课;
公众浮存加速;
公共浮动垂直轴;
公共浮动时间已过;
公众持股限额;
私有刚体2d rb;
//在第一帧更新之前调用Start
void Start()
{
rb=gameObject.GetComponent();
}
//每帧调用一次更新
无效更新()
{
verticalAxis=Input.GetAxisRaw(“垂直”);
如果(垂直轴>0&&timePassed==0)
{
isJumping=true;
}
}
私有void FixedUpdate()
{
如果(正在跳跃)
{
跳跃();
}
}
私有空间碰撞Stay2D(碰撞2D碰撞)
{
if(collision.collider.tag==“地面”)
{
isGrounded=true;
时间通过=0f;
}
}
私有无效跳转()
{
isfounded=false;
//修改球员刚体的y分量(跳跃)
矢量2速度=矢量2.up*加速*垂直轴;
rb.速度=速度;
//跳跃时计算时间
timePassed+=Time.deltaTime;
如果(时间通过>=跳限)
{
isJumping=false;
}
}
}

另外,正如stackoverflow所建议的那样,我试图缩小这个问题中的代码,但我不知道该删掉什么,因为根据我的说法,一切都是相关的,对解决问题至关重要。谢谢

如果您使用轴进行跳跃,您可以通过以下方式查看玩家是否已释放按钮:

if (GetAxis("Vertical") == 0.0) { //Button up
     // do stuff
}
if(GetAxis("Vertical") > 0)
{
     //Button down, is jumping
}

在Unity axis中,它们不是布尔值,而是系数,如果使用控制器,可以为不同的强度设置不同的行为

在不知道代码的其余部分的情况下,我会使用一个额外的标志,显然是一些用于获得冷却延迟的东西,如

public float jumpCoolDownTime;
private float coolDownTimer;
private bool canJump;

void Update()
{
    // if no can jump you can't jump again
    if(!canJump)
    {
        verticalAxis = Input.GetAxisRaw("Vertical");
        if (verticalAxis > 0)
        {
            canJump = false;
            isJumping = true;
            coolDownTimer = 0;
        }
    }
    else
    {
        // run timer to enable jump again
        if(isGrounded) coolDownTimer += Time.deltaTime;

        if(coolDownTimer >= jumpCoolDownTime)
        {
            canJump = true;
        }
    }
}

但是,您可以使用

public bool正在跳跃;
公众浮存加速;
公共浮动垂直轴;
公众持股限额;
公众假期;
私有刚体2d rb;
//在第一帧更新之前调用Start
void Start()
{
rb=gameObject.GetComponent();
}
无效更新()
{
//什么都不做已经是跳跃了
如果(正在跳跃)返回;
verticalAxis=Input.GetAxisRaw(“垂直”);
如果(垂直轴>0)
{
//起跳
start例程(Jump());
}
}
专用空心OnCollisionInter2D(碰撞2D碰撞)
{
如果(collision.collider.tag!=“地面”)返回;
isGrounded=true;
开始例行程序(冷却());
}
私有IEnumerator跳转()
{
isJumping=true;
isfounded=false;
冷却计时器=0;
var timePassed=0f;
while(通过的时间
我看不出玩家落地时你在计算的部分。汉克斯,我放弃了getaxis,而是使用了Input.GetMouseButtonDown,因为我已经读到该功能在mobile中接近Touch,目前它工作得很好。我从derHugo实现了按钮冷却解决方案,非常棒。但我想我不会用刚体来移动玩家,因为我发现你可以用触发器和运动学刚体来检测碰撞,而使用刚体的缺点是不平等跳跃,这是我几天来都无法解决的。谢谢你的帮助!
public bool isJumping;
public float speedUp;
public float verticalAxis;

public float jumpLimit;
public float coolDownTime;

private Rigidbody2D rb;

// Start is called before the first frame update
void Start()
{
    rb = gameObject.GetComponent<Rigidbody2D>();
}

void Update()
{
    // do nothing is already jumping
    if(isJumping) return;

    verticalAxis = Input.GetAxisRaw("Vertical");
    if (verticalAxis > 0)
    {
        // start jumping
        StartCoroutine(Jump());
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.tag != "Ground") return;

    isGrounded = true;
    StartCoroutine(CoolDown());
}

private IEnumerator Jump()
{
    isJumping = true;
    isGrounded = false;
    coolDownTimer = 0;

    var timePassed = 0f;
    while(timePassed < jumpLimit)
    {
        // wait for the FixedUpdate call
        yield return new WaitForFixedUpdate();

        Vector2 velocity = Vector2.up * speedUp * verticalAxis;
        rb.velocity = velocity;
    }
}

private IEnumerator CoolDown()
{
    yield return new WaitForSeoncds(coolDownTime);

    isJumping = false;
}