简体   繁体   English

如何为我在统一 2d 中的跳跃设置冷却时间? [找到答案]

[英]How do i make a cooldown for my jump in unity 2d? [found an answer]

I'm making a 2D platformer game.我正在制作一个 2D 平台游戏。 Everything is working out well except for the jump.一切都很好,除了跳跃。 The character jumps normally but the problem is there is no cooldown and I can basically jump infinitely.角色跳跃正常,但问题是没有冷却时间,我基本上可以无限跳跃。 Can someone please tell me how I can make a cooldown?有人可以告诉我如何进行冷却吗? Thanks.谢谢。 (I'm new to C# and Unity) (我是 C# 和 Unity 的新手)

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

public class MovingWithJump : MonoBehaviour
{

    public float speed;
    public float jump;
    public float cooldownTime;
    private float nextFireTime;
    private float move;
    private Rigidbody2D rb;
    private float jump_start_time = 0f;
    private float elapsed_jump_time = 0f;
    // Start is called before the first frame update
    void Start()
    {
       rb = GetComponent<Rigidbody2D>(); 
    }

    // Update is called once per frame
    void Update()
        
    {
        if(Input.GetButtonDown("Jump") && elapsed_jump_time > 1f) {
            rb.AddForce(new Vector2(rb.velocity.x, jump));
            jump_start_time = Time.time;
        }
        elapsed_jump_time = Time.time - jump_start_time;
       move = Input.GetAxis("Horizontal");

       rb.velocity = new Vector2(move * speed, rb.velocity.y);
       if(Input.GetButtonDown("Jump")) {
           rb.AddForce(new Vector2(rb.velocity.x, jump));
           
       }
    }
}


You can check the elapsed time since the jump to jump or not.您可以检查从跳转到跳转的经过时间。 Find the example for 2 seconds cooldown.找到 2 秒冷却时间的示例。 I did not debug that, but you can get the idea.我没有调试,但你可以明白。

private float jump_start_time = 0f;
private float elapsed_jump_time = 0f;

if(Input.GetButtonDown("Jump") && elapsed_jump_time > 2f) {
    rb.AddForce(new Vector2(rb.velocity.x, jump));
    jump_start_time = Time.time;         
}
elapsed_jump_time = Time.time - jump_start_time;

The easy way would be to include a boolean variable (true/false) and do a check to see i the boolean is true or not.简单的方法是包含一个 boolean 变量(真/假)并检查 boolean 是否为真。

For that to work though, you'll need to connect to the colliders in Unity.不过,要使其正常工作,您需要连接到 Unity 中的碰撞器。

So -所以 -

private bool isJumping;
public float speed;
public float jump;
public float cooldownTime;
private float nextFireTime;
private float move;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
   rb = GetComponent<Rigidbody2D>(); 
}

// Update is called once per frame
void Update()
    
{
    
   move = Input.GetAxis("Horizontal");

   rb.velocity = new Vector2(move * speed, rb.velocity.y);
   if((Input.GetButtonDown("Jump")) && !isJumping) {
       rb.AddForce(new Vector2(rb.velocity.x, jump));
       isJumping = true;
   }
   
}

After that though, you need a way to turn the boolean false.不过在那之后,您需要一种方法将 boolean 设为 false。 Because otherwise, you'll jump once, and never jump again.因为否则,你会跳一次,永远不会再跳。 So you need to grab the collider for the ground (or other similar object, preferably all on one layer) and when the onCollision triggers you can set the isJumping boolean to false.所以你需要抓住地面的对撞机(或其他类似的 object,最好都在一层上),当 onCollision 触发时,你可以将 isJumping boolean 设置为 false。

Add cooldown添加冷却时间

    public KeyCode JUMP_KEY;
    public const float COOLDOWN_TIME = 2.0f; 
    private float _cooldown;


    void Update(){
        _cooldown-= Time.deltaTime;
        if(Input.GetKey(JUMP_KEY) && Cooldown <= 0) {
            //TODO : JUMP which you want
            //Reset cooldown time
            _cooldown = COOLDOWN_TIME;
        }
    }

Detect Platform Collider检测平台碰撞器

by adding a small trigger area under the character to detect it on ground or not.通过在角色下方添加一个小的触发区域来检测它是否在地面上。 I prefer to create a new component like this我更喜欢像这样创建一个新组件


public class PlatformCollider : MonoBehaviour {
    public bool OnGround {get; private set;}

    private void OnTriggerEnter2D(Collider2D collision) {
        if(collision.tag == "ground") {
            OnGround = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision) {
        if(collision.tag == "ground") {
            OnGround = false;
        }
    }

}

When implement with 2 way above You will get JumpController class like this当使用上述两种方式实现时,您将获得 JumpController class 像这样


public class JumpController : MonoBehaviour
{
    public PlatformCollider GroundTrigger;
    
    public KeyCode JUMP_KEY;
    public const float COOLDOWN_TIME = 2.0f;
    private float _cooldown;
    
    void Update(){
        _cooldown -= Time.deltaTime;
        if(Input.GetKey(JUMP_KEY) && GroundTrigger.OnGround && _cooldown <= 0) {
            //TODO : JUMP which you want
            //Reset cooldown time
            _cooldown = COOLDOWN_TIME;
        }
    }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM