0
\$\begingroup\$

I have a dash mechanic, but it only seems to be working when the player is going up, down, left, right, or to diagonally to the upper right. It won't work diagonally in any other directions, and I can't figure out why?

The super weird thing is this problem only happens when my game is running on my computer, not on my laptop?? Any ideas on how I can fix this? Thank you!

void FixedUpdate()
    {
        if (dashing)
        {
            myRigidbody.AddForce(direction * dashspeed/3, 
            ForceMode2D.Impulse);
        }
        else
            myRigidbody.AddForce(direction* thrust/4.75f, 
            ForceMode2D.Impulse);
            myRigidbody.AddForce(direction * -thrust * 7f, 
            ForceMode2D.Force);
        }




void Update()
{
    direction.x = Input.GetAxisRaw("Horizontal");
    direction.y = Input.GetAxisRaw("Vertical");

    if (Input.GetKeyDown(KeyCode.Space) & powerSlider.value > 0)
      {
        if (direction.x != 0 || direction.y != 0)
      {
        if (CanDash) elapsedTime = 0f;
      }


if (elapsedTime < duration)
            {
                if (!dashing)
                {
                    CanDash = false;                                                         
                    dashing = true;                                    
                }               
                elapsedTime += Time.deltaTime;           
                }
      
        if (elapsedTime > duration)
        {                       
            if (dashing) 
            {                
                dashing = false;
                elapsedTime = Mathf.Infinity;
                StartCoroutine("DashCoolDown");              
            }
        }           

        if (elapsedTime >= duration)
        {
            elapsedTime = Mathf.Infinity;          
        }       
      
    IEnumerator DashCoolDown()
    {
        Debug.Log("dash cooldown");     
        yield return new WaitForSeconds(.45f);  
        CanDash = true;
    }   


}
\$\endgroup\$
4
  • \$\begingroup\$ Should both add force in the fixed update else branch be executed or only the first one? Your { } and your inline are a bit confusing. Because the third add force is always executed, even if you are dashing \$\endgroup\$
    – Zibelas
    Oct 27 '20 at 20:45
  • \$\begingroup\$ I meant for both of them to be executed at once, the reason why I have it that way is because I want it to feel like the player is underwater. I tried removing it the 3rd addforce just in case it's what was causing the problem, but that doesn't seem to be it. \$\endgroup\$
    – Ben
    Oct 27 '20 at 21:01
  • 1
    \$\begingroup\$ It could be the keyboard; some (non-gaming) keyboards don't let you press more then 3 or 4 key at once, and how many may also depend on which keys you're pressing. E.g., the one I'm typing this on now (non-gaming, fairly old) will detect if I press A+S+D+F (4 keys), but not A+S+D+F+G (5 keys), but it will detect A+S+D+R+T (5 keys). It detects F+G, but not F+G+H. Try it out here: en.key-test.ru \$\endgroup\$ Oct 27 '20 at 22:57
  • \$\begingroup\$ Wow I can't believe that was actually the problem! Thank you I have no idea how I would've figured that out myself. Now my question is- is this a common issue with many keyboards? Do I need to design my game around this problem? If so how? \$\endgroup\$
    – Ben
    Oct 28 '20 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.