所以我试图在我的游戏中实现两次跳跃,但这是行不通的。而现在,不知何故,我的球员不仅不能跳两次,甚至不能跳!
更新:他们现在可以跳了,但是仍然不能跳两次。
这是我的整个动作脚本:
using UnityEngine;
namespace Players
{
public class Actor : MonoBehaviour
{
//in order to control both players using 1 script.
public int playerIdx;
//Variables.
public float movementSpeed = 150f;
public float jumpForce = 250f;
//Ground stuff.
public LayerMask whatIsGround;
public bool grounded;
//boolean stuff.
private bool facingRight;
private bool moving;
//Needed to check if player is on the ground.
public Transform groundCheck;
//Limit player's movement speed.
public float maxMovementSpeed = 400f;
//Double jump stuff.
private bool doubleJumpReady;
//rb
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
doubleJumpReady = true;
rb = GetComponent<Rigidbody2D>();
facingRight = true;
}
// Update is called once per frame
void FixedUpdate()
{
SlowDown();
}
private void LateUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, whatIsGround);
if (grounded)
doubleJumpReady = true;
}
private void SlowDown()
{
if (moving) return;
//if player is not moving, slow them down.
if (rb.velocity.x > 0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * -Vector2.right);
if (rb.velocity.x < -0.2f)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right);
}
public void Move(int dir)
{
//Flip the player.
Flip(dir);
//Moving the player.
moving = true;
float xVel = rb.velocity.x; //Get x velocity.
if ( dir > 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir < 0)
rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
else if (dir == 0) { } //do nothing.
//Help player turn around faster.
if (xVel > 0.2f && dir < 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * -Vector2.right);
if (xVel < 0.2f && dir > 0)
rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * Vector2.right);
}
private void Flip(int dir)
{
if (facingRight && dir == -1 || !facingRight && dir == 1)
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
}
}
我不知道是因为我的跳转脚本还是我的播放器脚本
void Update()
{
if (playerIdx == 1)
{
if (Input.GetKey(KeyCode.A))
Move(-1);
if (Input.GetKey(KeyCode.D))
Move(1);
if (Input.GetKey(KeyCode.W))
Jump();
}
if (playerIdx == 2)
{
if (Input.GetKey(KeyCode.LeftArrow))
Move(-1);
if (Input.GetKey(KeyCode.RightArrow))
Move(1);
if (Input.GetKey(KeyCode.UpArrow))
Jump();
}
}
那我该如何解决呢?
答案 0 :(得分:2)
据我所知,您永远不会重置
doubleJumpReady = false;
可变。要解决此问题,只需将跳转代码更改为:
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
grounded = false;
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
}
}
希望它能起作用;)。
编辑: 接地由重叠的球体设置。因此,无需在此处进行设置。 使用此代码并按两次btn跳转,然后查看Debug.Log消息是否出现。另外,您的播放器ID(不需要idx。)据我所知,您的脚本已将两个附加到不同的对象。因此,无论如何都不会共享它们的变量。
protected void Jump()
{
if (grounded)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = true;
}
else if (!grounded && doubleJumpReady)
{
rb.AddForce(Vector2.up * jumpForce);
doubleJumpReady = false;
Debug.Log("I am double jumping");
}
}
最后一个问题是,您不执行一次跳跃,一次执行两次。 这是由于您的执行而发生的。
Input.GetKey(KeyCode.UP)
代替使用:
Input.GetKeyDown(KeyCode.Up);
GetKeyDown在按下按钮时返回true。 按下按钮时,GetKey返回true。
希望它现在可以正常工作;)
答案 1 :(得分:1)
我将使用计数器来实现它,您可以设置所需的跳转次数。
代码如下:
jumpCount = 0;
protected void Jump()
{
if(!grounded && jumpCount < 2)
{
jumpCount++;
rb.AddForce(Vector2.up * jumpForce);
}
if(grounded)
jumpCount = 0;
}
答案 2 :(得分:1)
假设您现在可以在阅读注释后再次执行正常跳转。我认为您不能“两次跳跃”的原因是,当您调用Jump()
方法时,您不仅会一次调用它,而且会两次调用它,所以发生的情况是玩家跳了起来然后立即加倍跳跃,因此您实际上并没有注意到发生了两次跳跃。您可以这样设置,使得doubleJumpReady
布尔值仅在您最初使用某种协例程或我曾经为某种双跳机制实现的某种东西跳过一段时间后才经过一段时间后才为真只有在玩家达到初始跳跃的最大高度或之后,才可以再次按下跳跃按钮以进行两次跳跃。