首页 > 解决方案 > Unity 新的输入系统和双跳本地多人游戏

问题描述

所以我在我目前正在开发的游戏中使用新的输入系统时遇到了一些麻烦。游戏将是某种类型的 Smash 游戏,我已经用旧的输入系统实现了二段跳,它可以工作,但现在我已经将其更改为新的,我可以再次在 Air 中跳一次我知道它一定是某种容易解决的问题,但我是初学者,我不太了解新的 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);
}

}

在这里你可以看到我的输入管理器设置

标签: c#unity3dinputlocalmultiplayer

解决方案


我认为问题在于执行第一次跳转的条件:

//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--;
    }
}

推荐阅读