问题描述
所以我在我目前正在开发的游戏中使用新的输入系统时遇到了一些问题。 游戏将是某种类型的 Smash 游戏,我已经使用旧的输入系统实现了双跳并且它起作用了,但现在我已经将其更改为新的,我可以再次在空中跳跃一次我知道这一定是某种容易解决的问题,但我是初学者,我不太了解新的 I-System。这是我的代码: 如果你能帮我解决这个问题,我真的很感激,最终我可以从你的评论中学到一些东西。 (顺便说一句,对不起我的英语我不是本地人哈哈)
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehavIoUr
{
public float speed;
public float jumpForce;
public float moveInput;
public float checkRadius;
private int extraJumps;
public int extraJumpsValue;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public LayerMask whatIsGround;
private Vector2 movementInput = Vector2.zero;
private bool jumped = false;
//OnMove Input to later set as Event in Input Manager
public void OnMove(InputAction.CallbackContext context)
{
movementInput = context.ReadValue<Vector2>();
}
//OnJump Input to later set as Event in Input Manager
public void OnJump(InputAction.CallbackContext context)
{
jumped = context.action.triggered;
}
//start
private void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
//check if isGrounded
isGrounded = Physics2D.OverlapCircle(groundCheck.position,checkRadius,whatIsGround);
moveInput = movementInput.x;
//Move Right/Left relative to moveInput Value (-1 till 1)
rb.veLocity = new Vector2(moveInput * speed,rb.veLocity.y);
//rotate Player Sprite relative to a / d Input
if(facingRight == false && moveInput > 0)
{
Flip();
}
else if(facingRight == true && moveInput <0)
{
Flip();
}
}
private void Update()
{
// if grounded set extra jumps u can take to a preset value
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
}
//if extrajumps is equal to 0 & player tries to jump and is grounded add upwards veLocity
if (extraJumps == 0 && jumped && isGrounded == true)
{
rb.veLocity = Vector2.up * jumpForce;
}
//subtract extra jump -1 and "jump"
else if (extraJumps > 0 && jumped)
{
rb.veLocity = Vector2.up * jumpForce;
extraJumps--;
}
}
//rotate player
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f,180f,0f);
}
}
解决方法
我认为问题在于执行第一次跳转的条件:
//When isGrouded then extraJumps equal 1 (in the case extraJumpsValue is 1)
if(isGrounded == true)
{
extraJumps = 1;
}
//The next condition is always false
//Because when isGrouded then extraJumps is never 0
if (isGrounded == true && extraJumps == 0 && /*Some other condition*/)
...
我不明白为什么要检查 extraJumps
以进行第一次跳转。我认为您可以删除此操作数:
if (/*extraJumps == 0 &&*/ jumped && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
完整代码:
private void Update()
{
// if grounded set extra jumps u can take to a preset value
if(isGrounded == true)
{
extraJumps = extraJumpsValue;
}
//if player tries to jump and is grounded add upwards velocity
if (jumped && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
//subtract extra jump -1 and "jump"
else if (extraJumps > 0 && jumped)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
}
}