1

我现在正在开发一个 2.5D 播放器控制器,但我在半空中的仪表板有问题。破折号有效,但在破折号完成后,角色无法在 X 轴上完全控制,直到他们撞到地面。我希望玩家在破折号后立即完全控制 X 轴,以便他们可以适当地躲避。

   void Update()
{
    PlayerInput();       
}

void FixedUpdate()
{
    xMovement();
    yMovement();
}

void PlayerInput()
{
    //Registers X and Y movement
    horizontalInput = Input.GetAxis("Horizontal");
    verticalInput = Input.GetAxis("Vertical");

    if (Input.GetButton("Run"))
    {
        isRunning = true;
    }
    else if (Input.GetButtonUp("Run"))
    {
        isRunning = false;
    }

    //Makes player jump by returning a bool value to "yMovement()" when pressed.
    if (Input.GetButtonDown("Jump"))
    {
        jumpRequest = true;
    }

    if (Input.GetKeyDown(KeyCode.A))
    {
        if (doubleTapTime > Time.time && lastKeyCode == KeyCode.A)
        {
            StartCoroutine(Dash(1f));
            Debug.Log("You dashed left");
        }
        else
        {
            doubleTapTime = Time.time + 0.5f;
        }

        lastKeyCode = KeyCode.A;
    }
       

    if (Input.GetKeyDown(KeyCode.D))
    {
        if (doubleTapTime > Time.time && lastKeyCode == KeyCode.D)
        {
            StartCoroutine(Dash(-1f));
            Debug.Log("You dashed right");
        }
        else
        {
            doubleTapTime = Time.time + 0.5f;
        }

        lastKeyCode = KeyCode.D;
    }
}

void xMovement()
{
    //Makes player walk left and right

    if (!isRunning && !isDashing)
    {
        transform.Translate(Vector3.left * walkSpeed * horizontalInput * Time.deltaTime);
    }

    //Makes player run left and right
    else if (isRunning && !isDashing)
    {
        transform.Translate(Vector3.left * runSpeed * horizontalInput * Time.deltaTime);
    }            
}

void yMovement()
{
    //Make player jump when Jump is pressed
    if (jumpRequest)
    {
        playerRb.velocity = new Vector3(playerRb.velocity.x, jumpForce);
        jumpRequest = false;
        //playerRb.velocity = new Vector2(playerRb.velocity.x, playerRb.velocity.y * jumpForce);
    }

    //Makes player fall faster in general, and when the Jump button is released
    if (playerRb.velocity.y < 0)
    {
        playerRb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplyer - 1) * Time.deltaTime;
    }
    else if (playerRb.velocity.y > 0 && !Input.GetButton("Jump"))
    {
        playerRb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplyer - 1) * Time.deltaTime;
    }
}

IEnumerator Dash(float direction)
{
    isDashing = true;
    playerRb.velocity = new Vector3(playerRb.velocity.x, .0f);
    playerRb.velocity = new Vector3(dashDistance * direction, 0f, 0f);
    playerRb.useGravity = false;
    yield return new WaitForSeconds(.2f);
    isDashing = false;
    playerRb.useGravity = true;

任何关于代码优化的提示也非常感谢。我对编码还很陌生,我宁愿在必须改掉坏习惯之前学习适当的编码习惯。谢谢!

4

1 回答 1

0

我认为您的问题是,transform当您在 yAxis 上实际使用速度时,您在 x 轴上使用了 Translation。Unity 可能无法在单个“FixedUpdate”调用中处理这两个问题。或者它可能只是不符合您的预期。

我建议坚持速度变化。所以这会给出类似的东西

void xMovement()
{
    //Makes player walk left and right

    if (!isRunning && !isDashing)
    {
        playerRb.velocity += Vector3.left * walkSpeed * horizontalInput * Time.deltaTime;
    }

    //Makes player run left and right
    else if (isRunning && !isDashing)
    {
        playerRb.velocity += Vector3.left * runSpeed * horizontalInput * Time.deltaTime;
    }            
}
于 2021-04-29T16:35:01.073 回答