Cant make player have some X-velocity upon dying

Here’s my code, and things work just like in Rick but I want my player to fly a bit away from the enemy too, but all I can manage is making him jump off a bit… How do I make my player have some X velocity?

void Update()
{
    Die();
}

void Die()
{
    if (myCapsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Enemy")) && isAlive)
    {
        myPlayerInput.enabled = false;
        myAnimator.SetBool("isJumping", false);
        myAnimator.SetBool("isRunning", false);

        // Chạy animation và deadKick tuỳ vào hướng va chạm
        // Nếu nhân vật đang quay phải
        if (myTransform.localScale.x >= 0)
        {
            myRigidbody2D.velocity = deadKickFromLeft;

            Debug.Log("X velocity is: " + myRigidbody2D.velocity.x);

            // trigger animation 
            myAnimator.SetTrigger("isDeadLeft");

            // chết r thì vô chạm và rơi khỏi trò chơi luôn - nếu thích giữ lại ng lại khi chết thì cmt 2 dòng này lại
            myCapsuleCollider2D.isTrigger = true;
            myCircleCollider2D.isTrigger = true;

            myRigidbody2D.gravityScale = baseGravity;
            isAlive = false;
            Invoke("reloadScene", waitTillReload);
        }
        // Nếu nhân vật đang quay trái
        else if (myTransform.localScale.x < 0)
        {

            Debug.Log("X velocity is: " + myRigidbody2D.velocity.x);

            myRigidbody2D.velocity = deadKickFromRight;

            myAnimator.SetTrigger("isDeadRight");

            // chết r thì vô chạm và rơi khỏi trò chơi luôn - nếu thích giữ lại ng lại khi chết thì cmt 2 dòng này lại
            myCapsuleCollider2D.isTrigger = true;
            myCircleCollider2D.isTrigger = true;

            myRigidbody2D.gravityScale = baseGravity;
            isAlive = false;
            Invoke("reloadScene", waitTillReload);
        }

        else
        {
            return;
        }

    }
}

Hi,

From what I see, your code overrides the player’s velocity with this line:
myRigidbody2D.velocity = deadKickFromRight;.

If you want to give your player x-velocity, I would suggest to start with deadKickFromRight. If you exposed this variable in the Inspector, change the values there.

// Khai báo Vector lực đẩy khi chết
    [SerializeField] Vector2 deadKickFromLeft = new Vector2(10f, 10f);
    [SerializeField] Vector2 deadKickFromRight = new Vector2(-10f, 10f);

I have changed it like a thoudsand times already, from 1 to 1000, nothing happens, player only have Y-velocity, i even debug.log the X velocity in die() to see if my X-velocity is affecting, it does! X velocity is 1000 in my console when he dies, but still no X velocity on my player :sob:

Where did you change it? In the Inspector? In your code?

And did you check if something else in your code overrides the player’s velocity after the deathKick velocity was applied?

i try changing it both at the inspector and in my code, not helping, could be that im overriding it somewhere else in my code but i just can’t find it, is there any way to look for possible code overrides?

In Rick’s original code, we override the velocity with = and +=, for example, in the OnRun(), OnJump() and OnClimb() methods (or whatever the names are in your class).

If you apply x-velocity and if Debug.Log("X velocity is: " + myRigidbody2D.velocity.x); shows the expected value, the code is very likely ‘correct’ up to this line. However, it might be that Unity calls other methods after you applied the x-velocity. And those other methods could, for example, set the x-velocity to 0.

Use Debug.Logs to figure out if other parts of your code get executed after you applied the x-velocity. When the player is dead, OnRun(), OnJump() and so on should not get executed anymore.

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

Privacy & Terms