如何使用Input.GetButton()而不同时获得多个跳转?统一二维字符控制器C#
我已经制作了大量的2D游戏,你是屏幕上的一个物体,你可以在屏幕上四处跳跃等等,但是在我最新的项目“三角奔跑”(geometry dash,但更糟糕的是),我希望能够按住鼠标,让角色以均匀的速度跳跃,你知道,就像在geometry dash中一样,因此,我没有使用如何使用Input.GetButton()而不同时获得多个跳转?统一二维字符控制器C#,c#,unity3d,C#,Unity3d,我已经制作了大量的2D游戏,你是屏幕上的一个物体,你可以在屏幕上四处跳跃等等,但是在我最新的项目“三角奔跑”(geometry dash,但更糟糕的是),我希望能够按住鼠标,让角色以均匀的速度跳跃,你知道,就像在geometry dash中一样,因此,我没有使用Input.GetButtonDown而是用Input.GetButton替换它,然后我放了一个返回在if语句之后,因此每个Update()只能运行一个if(这是它的函数),但这使得除了最快的点击之外,任何东西都可以启动播放器,使其达到正
Input.GetButtonDown
而是用Input.GetButton
替换它,然后我放了一个返回代码>在if
语句之后,因此每个Update()
只能运行一个if
(这是它的函数),但这使得除了最快的点击之外,任何东西都可以启动播放器,使其达到正常跳跃高度的10-20倍。我尝试了很多不同的方法来确保if
语句在Update()
中只运行一次,这让charactercontroller有足够的时间来检查玩家是否被禁足以允许或阻止跳转,所以我不确定它为什么要启动我。编辑:使用Input.GetButtonDown
时,一切正常
这是到目前为止玩家移动的全部代码,我刚刚开始游戏,所以现在还没有太多:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
bool jump = false;
public Rigidbody2D rb;
public float moveSpeed = 10f;
public Transform transform;
private float moveY = 0f;
private int jumpCounter = 0;
private bool jumpIf = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Jump") && jumpIf) // if we press the jump button
{
jump = true; // set jump bool to true (see void fixedupdate for more)
jumpIf = false;
}
else
{
jump = false;
}
jumpIf = true;
return;
}
void FixedUpdate() // called a set amount of times per second, used with physics movement etc lol
{
controller.Move(0f, false, jump); // move in which direction, are we crouching, are we jumping
jump = false;
moveY = rb.velocity.y;
rb.velocity = new Vector2(moveSpeed, moveY);
}
}
正如您所看到的,在if
中,它非常混乱,因为我最后尝试了各种各样的事情
非常感谢 您是否尝试过在FixedUpdate
中对if语句使用GetButtonUp
?
我在Update
中遇到了类似的交互问题。使用Update
上的GetButton
将使游戏在开始按跳转键后每帧执行一次跳转,而使用GetButtonUp
则仅当跳转键被释放时才会执行该操作。
类似的方法可能会奏效:
void FixedUpdate()
{
if (Input.GetButtonUp("Jump")) // if we press the jump button
{
jump = true; // set jump bool to true, in case you want to use it for an animation,etc.
//Jump code here.
controller.Move(0f, false, jump);
moveY = rb.velocity.y;
rb.velocity = new Vector2(moveSpeed, moveY);
}
}
或者在更新中使用产生返回新的WaitForSeconds()
如果什么都不做,你的跳转。。它每帧重置一次,因此您的跳转
设置为真
每帧。。。我不知道在角色控制器中跳跃是如何处理的,但我想如果你只在FixedUpdate中调用它一次,实际上应该没问题,因为在内部,它有一个固定的检查。。但也许接地半径太大了?听起来OP更希望用户能够简单地按住跳跃按钮,在玩家物体接触地面时进行单次跳跃。。。这就是切换到GetButton
而不是使用GetButtonDown