微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

玩家在空中冲刺后几乎无法控制角色

如何解决玩家在空中冲刺后几乎无法控制角色

我现在正在开发 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;

也非常感谢有关代码优化的任何提示。我对编码还是很陌生,宁愿先学习适当的编码习惯,然后才能忘记坏习惯。谢谢!

解决方法

我认为您的问题是您在 x 轴上使用 transform 上的平移,而实际上在 yAxis 上使用的是速度。 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;
    }            
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。