[英]How do i make a cooldown for my jump in unity 2d? [found an answer]
我正在制作一個 2D 平台游戲。 一切都很好,除了跳躍。 角色跳躍正常,但問題是沒有冷卻時間,我基本上可以無限跳躍。 有人可以告訴我如何進行冷卻嗎? 謝謝。 (我是 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));
}
}
}
您可以檢查從跳轉到跳轉的經過時間。 找到 2 秒冷卻時間的示例。 我沒有調試,但你可以明白。
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;
簡單的方法是包含一個 boolean 變量(真/假)並檢查 boolean 是否為真。
不過,要使其正常工作,您需要連接到 Unity 中的碰撞器。
所以 -
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;
}
}
不過在那之后,您需要一種方法將 boolean 設為 false。 因為否則,你會跳一次,永遠不會再跳。 所以你需要抓住地面的對撞機(或其他類似的 object,最好都在一層上),當 onCollision 觸發時,你可以將 isJumping boolean 設置為 false。
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;
}
}
通過在角色下方添加一個小的觸發區域來檢測它是否在地面上。 我更喜歡像這樣創建一個新組件
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;
}
}
}
當使用上述兩種方式實現時,您將獲得 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.