1

I'm working on a 2.5D player controller right now, and there is a problem with my dash in midair. The dash works, but the character can't be completely controlled on the X axis after the dash is finished until they hit the ground. I want the player to have full control on the X axis right after the dash so that they can dodge appropriately.

   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;

Any tips on code optimization is also greatly appreciated. I'm still fairly new to coding and would rather learn appropriate coding habits before I have to unlearn bad ones. Thank you!

  • Hi, you can try and add more context or provide any type of demo that others can use to help reproduce and help out. – Pimenta Apr 26 '21 at 17:22
  • The player can dash and then be controlled as soon as the dash is finished so long as they are on the ground. The problem is when the player tries to dash in midair. The player can dash without any problems in the air, but upon finishing the dash the player is unable to stop their momentum or change direction until they land on the ground. It's almost as if the dash is "dragging" them through the air until they hit the ground. – Kryptic Kralo Apr 27 '21 at 02:08
  • I want the player to be able to maintain that momentum if they wish, but I also don't want to *force* them to move forwards until they land. – Kryptic Kralo Apr 27 '21 at 02:14

1 Answers1

0

I think your issue is that you're using a Translation on transform for the x axis when on the yAxis you're actually using the velocity. Unity might have trouble dealing with both in a single "FixedUpdate" call. Or it might just not do what you expect.

I would recommend sticking to velocity changes. So that would give something like

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;
    }            
}
dekajoo
  • 2,024
  • 1
  • 25
  • 36