如何让角色在 Unity 3D 中一次只跳一次? [英] How to make a character jumping with just one jump at a time in Unity 3D?

查看:31
本文介绍了如何让角色在 Unity 3D 中一次只跳一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个脚本来让我的角色跳跃,但是当我一直按下按钮时它会继续跳跃.我只是想让它跳一次.

I created a script to make my character jump but it keeps jumping when I keep pressing the button. I just want it to jump once a time.

using UnityEngine;
[RequireComponent(typeof(Rigidbody))]

public class Jump : MonoBehaviour {
   Rigidbody rb;
   void Start()
   {
       rb = GetComponent<Rigidbody>();  
   }
   void Update()
   {
      if (Input.GetKeyDown(KeyCode.Space))
      {
         rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
      }
   }
}

推荐答案

添加、创建向量、碰撞器和布尔值

To add, create vectors, colliders, and boolean

private BoxCollider2D box;
Vector3 max = box.bounds.max;
Vector3 minValue = box.bounds.minValue;

Vector2 x = new Vector2(max.x, minValue.y -0.1f);
Vector2 y = new Vector2(minValue.x, minValue.y - 0.1f;

Collider2D ground = Physics2D.OverlapArea(x,y);
bool jump = (ground != null) ? true : false;
if (jump && Input.GetKeyDown(KeyCode.Space)) {
    //jump
}

只需添加 Vectors 来检查玩家所在的位置,并添加一个 Collider 来检查玩家是否在地面上,以及确定是否允许跳跃的 bool,并将 bool 放入 Input.GetKeyDown.如果 bool 为 true 并且 key 被按下,则跳转.

Just add Vectors to check where player is located and a Collider to check if player is on ground, and the bool that determines if jumping is allowed, and put the bool in with Input.GetKeyDown. If bool is true and key is pressed, then jump.

这篇关于如何让角色在 Unity 3D 中一次只跳一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆