问题描述
我遵循了Roll-A-Ball"的 Unity 教程,并添加了一些扩展.球现在可以跳跃了,但是每次跳跃的次数太多了;当我按下 SPACE 时,我一次只想要一次球.现在我可以按 3 次 SPACE,它会跳得越来越高.
I've followed the Unity tutorial for "Roll-A-Ball", and have added some extensions. The ball can now jump, but it jumps too many times each jump; I just want the ball once at a time when I press on SPACE. Now I can press on SPACE 3 times an it'll jump higher and higher.
if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 jump = new Vector3(0.0f, 150.0f, 0.0f);
rb.AddForce(jump);
}
尝试稍微更改代码,但现在我只能跳一次(第一次)
Have tried changing the code a little bit, but now I can ONLY jump once (the first time)
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (Input.GetKeyDown(KeyCode.Space) && jump.y <= 0.0f)
{
jump.Set(0.0f, 150f, 0.0f);
rb.AddForce(jump);
}
}
推荐答案
我找到了解决方案.解决方案是添加 OnCollisionEnter
,您可以在其中处理玩家与地面碰撞时会发生的情况.此外,我添加了一个计数器,当玩家跳跃时设置为 0,当他与地面碰撞时设置为 0.
I've found a solution. The solution is to add OnCollisionEnter
where you can handle what will happen when the player collides with the ground. Furthermore I've added a counter which is set to 0 when the player jumps and 0 again when he collides with the ground.
计数器:
if (Input.GetKeyDown(KeyCode.Space))
{
if (JumpCount < 1)
{
Vector3 jump = new Vector3(0.0f, 250.0f, 0.0f);
rb.AddForce(jump);
JumpCount++;
}
}
这里的计数器设置为 0:
The counter is set to 0 here:
void OnCollisionEnter(Collision other)
{
JumpCount = 0;
}
这篇关于如何解决双跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!