[英]Jump timer in unity 3d and how to define spacebar in C#
I was wondering if anybody knew how to set up a jump limit or jump timer so that users cant spam jump on unity.我想知道是否有人知道如何设置跳跃限制或跳跃计时器,以便用户不能在 unity 上垃圾邮件跳跃。 this is my first time using C# and I'm not sure how to use it properly yet here is my code
这是我第一次使用 C#,我不确定如何正确使用它,但这是我的代码
Also, if anybody knows how to define the spacebar instead of using the w key that would be great.另外,如果有人知道如何定义空格键而不是使用 w 键,那就太好了。
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float ForwardForce = 800f;
public float SideWaysForce = 300f;
public float JumpForce = 200f;
public LayerMask GroundLayers;
public BoxCollider col;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//change forward force
rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
if ( Input.GetKey("d") )
{
rb.AddForce(SideWaysForce, 0, 0 * Time.deltaTime);
}
if (Input.GetKey("a"))
{
rb.AddForce(-SideWaysForce, 0, 0 * Time.deltaTime);
}
if (Input.GetKey("w"))
{
rb.AddForce(0, JumpForce, 0 * Time.deltaTime);
}
}
}
Spacebar input: Input.GetKey("space")空格键输入:Input.GetKey("space")
Create a bool and track if user is jumping based on if the height is more than the original height.创建一个 bool 并根据高度是否大于原始高度来跟踪用户是否正在跳跃。 Of course you can change/customize/optimize this so suit the needs of your game
当然,您可以更改/自定义/优化它以满足您的游戏需求
bool isJumping;
float originalPositionY;
void Start() {
rb = GetComponent<Rigidbody>();
originalPositionY = rb.position.y;
../
if (rb.position.y > originalPositionY) {
isJumping = true;
} else {
isJumping = false;
if (Input.GetKey("space") && !isJumping)
{
rb.AddForce(0, JumpForce, 0 * Time.deltaTime);
}
../
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.