0
\$\begingroup\$

I'm trying to write a script where when the player (kinematic) comes up against an enemy he stops, jumps and gets dynamic again. The problem is that when it returns dynamic it is thrown away and does not fall towards the floor. I keep looking but I can't find a solution.

This is the code I wrote:

private void OnCollisionEnter2D(Collision2D collision)

{
    if(collision.collider.tag == "Enemy")
    {
        rb.velocity = Vector2.zero;

        rb.isKinematic = false;

        Jump(Vector2.up);
        //rb.isKinematic = false;
    }
}

private void Jump(Vector2 dir)
{
    rb.velocity += dir * jumpForce;
}
\$\endgroup\$
  • \$\begingroup\$ Have you tried velocity = rather than +=? It's possible your kinematic body has accumulated a larger velocity than you expect. Another possibility is that the player's collider is embedded in the ground/enemy at the time of the switch, forcing the physics engine to apply a large impulse to separate them. Have you tried changing its physics layer or using IgnoreCollision to eliminate this possibility? \$\endgroup\$ – DMGregory Aug 23 at 13:38

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.