![](/img/trans.png)
[英]Limit the number of times a button can be pressed - C# ASP.NET MVC5 JavaScript
[英]How to limit the number of time a button pressed in Unity C#
我正在制作 FPS 游戲,當我添加跳躍物理時,我遇到了一個問題。 就是每按一次空格鍵,玩家的跳躍高度就會增加。 我希望玩家在地面限制時按下空格鍵的次數為 1。這是我寫的代碼:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float moveSpeed;
Vector3 velocity;
public float gravity;
public Transform groundCheck;
public LayerMask groundMask;
public float groundDistance = 0.2f;
bool isGrounded;
public float jumpHeight;
public float jumpButtonPressedNum = Input.GetButtonDown("Jump");
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0 )
{
velocity.y = 0f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * moveSpeed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (Input.GetButtonDown("Jump") == 1)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
}
您需要檢查您的播放器是否在地面上。
if (isGrounded && Input.GetButtonDown("Jump") == 1)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
我之前犯過這個錯誤 - 你需要使用你制作的isGrounded
bool 來測試你是否接地,如果你是你可以跳躍,但如果你不接觸地面你不能跳躍(因為你不是接地)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.