Need Assistance creating a dash function in my 2d Unity Game

I Have Been trying all day to create a functional dash code. I’m almost there, but i keep encountering a bug where my dash happens sporadically, seemingly triggering randomly when i hit the corresponding button. sometimes it triggers, some times it doesn’t. i would appreciate any help on the topic.

Here is the code:

public class Player : MonoBehaviour
{
//Config
[SerializeField] float runSpeed = 5f;
[SerializeField] float dashSpeed = 15;

//States
bool isAlive = true;

//Cashed References
Rigidbody2D myRigidBody;
Animator myAnimator;
CapsuleCollider2D myBodyCollider;

// Methods and Messages

void Start()
{
    myRigidBody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    myBodyCollider = GetComponent<CapsuleCollider2D>();
}


void Update()
{
    Run();
    FlipSprite();
    
}

private void Run()
{
    float controlThrowHorizontal = Input.GetAxisRaw("Horizontal"); // value is betweeen -1 to +1
    float controlThrowVertical = Input.GetAxisRaw("Vertical"); // value is betweeen -1 to +1
    Vector2 playerVelocity = new Vector2(controlThrowHorizontal * runSpeed  , controlThrowVertical*runSpeed );
    myRigidBody.velocity = playerVelocity;

    if (Input.GetKeyDown(KeyCode.Joystick1Button0) || Input.GetKeyDown("space"))
    {
        if(myRigidBody.velocity != new Vector2(0,0))
        {
            Debug.Log("space was pressed");
            myRigidBody.velocity = playerVelocity * dashSpeed;

        }
        else
        {
            Debug.Log("dash from none");
            playerVelocity = new Vector2(0, 5) * dashSpeed;
            myRigidBody.velocity = playerVelocity;
        }
    }

   

    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    bool playerHasVerticalSpeed = (myRigidBody.velocity.y) > Mathf.Epsilon;
    bool playerHasNegativeVerticalSpeed = (myRigidBody.velocity.y) < -.01;
    myAnimator.SetBool("isRunningDown", playerHasNegativeVerticalSpeed);
    myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
    myAnimator.SetBool("isRunningUp", playerHasVerticalSpeed);

   
}
private void FlipSprite()
{
    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    if (playerHasHorizontalSpeed)
    {
        transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1);
    }
}

}

*note: I am getting no errors of any kind.

Any help is appreciated.

Hi Pengwini,

Welcome to our community! :slight_smile:

For which project is this code? In which section and lecture are you? And what is “functional dash code”?

This is actually part of a personal project, however I used the movement code from the Unity 2D “Tilevania” section. It is a top-down action adventure, and by dash code I mean a code that causes instantaneous movement forward to the player character.

You could test the AddForce method with the Impulse mode.

https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

Also please feel free to ask our helpful community of students for advice in our official Discord chat.

Try my code. You should give a speed some big value for this to work.

void Run()
{
float myVelocityX = myRigidbody.velocity.x;
float myVelocityY = myRigidbody.velocity.y;
float absHorizontal = Mathf.Abs(Input.GetAxis("Horizontal"));
float absVertical = Mathf.Abs(Input.GetAxis("Vertical"));
if ( absHorizontal > 0.2f && Input.GetKeyDown(KeyCode.Space))
{ 
myVelocityX = Input.GetAxis("Horizontal") * dashSpeed * Time.deltaTime;
}
else if(absHorizontal > 0.2f)
{
myVelocityX = Input.GetAxis("Horizontal") * runSpeed * Time.deltaTime;
}
if ( absVertical > 0.2f && Input.GetKeyDown(KeyCode.Space))
{ 
myVelocityY = Input.GetAxis("Vertical") * dashSpeed * Time.deltaTime;
}
else if ( absVertical > 0.2f)
{
myVelocityY = Input.GetAxis("Vertical") * runSpeed * Time.deltaTime;
}
myRigidbody.velocity += new Vector2( myVelocityX, myVelocityY);

 bool playerHasHorizontalSpeed = Mathf.Abs(myVelocityX) > Mathf.Epsilon;
 bool playerHasVerticalSpeed = (myVelocityY) > Mathf.Epsilon;
 bool playerHasNegativeVerticalSpeed = (myVelocityY) < -.01;
 myAnimator.SetBool("isRunningDown", playerHasNegativeVerticalSpeed);
 myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
 myAnimator.SetBool("isRunningUp", playerHasVerticalSpeed);
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms