【发布时间】:2018-01-10 22:11:41
【问题描述】:
好的,我正在构建一个具有两种跳跃类型的平台游戏。 正常跳跃 - 在地面上时,按空格键 空中跳跃 - 跳跃时,有限的额外跳跃次数(同时按下空格键)
我已经制定了这次跳跃的主要机制,但我遇到了障碍。每次跳跃时,有限的空中跳跃次数从 3 次开始。但是,在我编写的代码中,Unity 使用初始“正常跳转”作为三者之一。
下面是我的代码示例:
bool pressingJumpButton = Input.GetKeyDown("space");
if (pressingJumpButton)
{
if (canJump)
{
jumping = true;
}
}
if (jumping)
{
if (pressingJumpButton && inAir == false) {
Debug.Log("jump");
GetComponent<Rigidbody>().velocity = new Vector3(
GetComponent<Rigidbody>().velocity.x,
jumpingSpeed,
GetComponent<Rigidbody>().velocity.z);
inAir = true;
}
}
//airJump logic
if(inAir==true)
{
airJump = true;
if (airJump && maxAir>0 && pressingJumpButton)
{
Debug.Log("airJump");
maxAir -= 1;
GetComponent<Rigidbody>().velocity = new Vector3(
GetComponent<Rigidbody>().velocity.x,
jumpingSpeed,
GetComponent<Rigidbody>().velocity.z);
}
}
// on the floor
void OnTriggerStay (Collider otherCollider)
{
if (otherCollider.tag =="JumpingArea")
{
canJump = true;
jumping = false;
inAir = false;
maxAir = 3;
airJump = false;
}
}
我将 debug.logs 放入以查看是否可以找到问题,并且当我进行第一次跳跃(从地面)时,它会同时注册为正常跳跃和空中跳跃。
我认为只有在按下跳转按钮一次后才返回 true 的 inAir 布尔值将是解决方法,但没有运气。
我做错了什么?
【问题讨论】:
-
如何更改
pressingJumpButton的值? -
“在我编写的代码中,Unity 使用初始的“正常跳转”作为三个之一“ - 如果这确实是原因,那么为什么不将
maxAir更改为 @ 987654324@ 并将其增加到 4?这将节省您实施逻辑以解决您是否在第一次跳跃。 -
嗨 Ant,我想过这个问题,但是 airJump 变量将在游戏中升级,我认为如果我不将它分开,以后可能会给我带来更多问题。跨度>
-
我已将我的 pressJumpButton 代码添加到 OP
-
如果我理解正确,当你按空格键时,布尔值就会变为真。你在地上,所以你得到了“跳跃”日志。但现在你在空中,但 bool 仍然是真的。所以你会得到“airJump”日志。