1
\$\begingroup\$

enter image description here

So, I have this problem I am facing, for some reason my player does not get a knockback when it stays at the right side of the enemy but when it stays at the left side it gives a knockback. What's the problem?


PLAYER MOVEMENT


void Movement()
    {

        //THIS IS FOR WALKING   
        if (isControlesEnabled)
        {
            moveInput = Input.GetAxisRaw("Horizontal") * runSpeed;
            myBody.velocity = new Vector2(moveInput, myBody.velocity.y);
        }

PLAYER KNOCKBACK


private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            health -= 0;

            if (health <= 0)
            {
                gameObject.SetActive(false);
            }
            else
            {
                ContactPoint2D contactPoint = collision.GetContact(0);
                Vector2 playerPosition = transform.position;
                Vector2 dir = contactPoint.point - playerPosition;

                dir = -dir.normalized;

                myBody.velocity = new Vector2(0, 0);
                myBody.inertia = 0;

                isControlesEnabled = false;
                Invoke("EnablePlayerControles", controlesDisablePeriod);

                myBody.AddForce(dir * knockBackForce, knockBackForceMode);
            }
        }
    }

    private void EnablePlayerControles()
    {
       isControlesEnabled = true;
    }
\$\endgroup\$
  • \$\begingroup\$ Try this line of code: "myBody.velocity = dir * knockBackForce;" and comment out "myBody.velocity = dir * knockBackForce;" and check if it works. As both of them work correctly with in my project. \$\endgroup\$ – Swati Nov 6 at 9:44
  • \$\begingroup\$ sorry my mistake change " myBody.AddForce(dir * knockBackForce, knockBackForceMode);" of code with "myBody.velocity = dir * knockBackForce;". \$\endgroup\$ – Swati Nov 6 at 11:54
  • \$\begingroup\$ It's still not working tho D: could this be the assets problem or something? \$\endgroup\$ – Saidus Nov 6 at 12:02
  • \$\begingroup\$ Will to be honest i have no idea why its happening. But there is another thing that I think of is that the player got knocked in wrong direction.I will conform this after office time as currently i am in office so i will do it in my free time and i will let know when its done. Also another thing that might help is record "Scene View" video in which transform Arrows of the player is visible. \$\endgroup\$ – Swati Nov 6 at 12:11
  • \$\begingroup\$ No worries, I can wait. I also recorded "Scene View". I edited the gif above. \$\endgroup\$ – Saidus Nov 6 at 12:34
1
\$\begingroup\$

Do your entities both use box colliders?

When two axis-aligned box colliders collide, then there isn't really a single collision point but rather a collision line on which the colliders touch. Unity is supposed to return multiple collision points in such a situation. That's why Collision2d.contacts is an array and not a single value. But your code only cares about the very first collision point in that array (ContactPoint2D contactPoint = collision.GetContact(0);).

That point happens to be the lowest point on left collisions and the highest point on right collisions. So a left-collision launches your player upwards (when it should get launched horizontally) while a right-collision launches the player downwards. The downwards trajectory pushes the player into the ground where friction kills most of its momentum, so it seems like right-collisions do almost nothing at all.

How do you fix that problem?

Instead of comparing the player position with the contact point, compare the player position with the center of the collider.

else
{
    Vector2 enemyPosition = collision.transform.position;
    Vector2 playerPosition = transform.position;
    Vector2 dir = enemyPosition  - playerPosition;

When the positions of both objects are on the same height then you will get a horizontal motion. If you would like to maintain the slight upwards trajectory (I personally think it's quite neat), subtract a little from the height of the enemy position. This example subtracts 0.5 units. You might have to experiment a bit with the value to find the one which gives you the best results:

Vector2 enemyPosition = collision.transform.position - new Vector3(0.0f, 0.5f);

I am looking forward to playing your game.

\$\endgroup\$
  • \$\begingroup\$ I would love to add the upwards trajectory. It adds more features to the game. What do you mean by "subtracting little from the z-axis"? Thanks, I hope I can create my first complete game! \$\endgroup\$ – Saidus Nov 6 at 13:19
  • \$\begingroup\$ @Saidus Answer updated. \$\endgroup\$ – Philipp Nov 6 at 13:27
  • \$\begingroup\$ The code line you gave gives an error for some reason. \$\endgroup\$ – Saidus Nov 6 at 13:31
  • \$\begingroup\$ @Saidus What error? \$\endgroup\$ – Philipp Nov 6 at 13:32
  • \$\begingroup\$ Nvm, I was able to fix it. The "- new vector" shouldn't be two but three. \$\endgroup\$ – Saidus Nov 6 at 13:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.