private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            enemy Enemy = collision.gameObject.GetComponent <enemy>();
            if (state == State.falling)
            {
                //Destroy(collision.gameObject);
                Enemy.JumpedOn();
            }
            else
            {
                state = State.hurt;
                if (collision.gameObject.transform.position.x > transform.position.x)
                {
                    // This means enemy is on the right
                    // Then throw me on the left
                    rb.velocity          = new Vector2(-hurt_speed + 0.5f * rb.velocity.x, 0.5f * rb.velocity.y + hurt_speed);
                    transform.localScale = new Vector2(-1, 1);

                    print("shall be sent left !");
                }
                else
                {
                    // Enemy is on the left
                    // Then throw me on the right
                    rb.velocity          = new Vector2(+hurt_speed + 0.5f * rb.velocity.x, 0.5f * rb.velocity.y + hurt_speed);
                    transform.localScale = new Vector2(1, 1);

                    print("shall be sent right !");
                }
            }
        }
    }
Пример #2
0
 private void OnCollisionEnter2D(Collision2D other)
 {
     if (other.gameObject.tag == "enemy")
     {
         enemy enemy = other.gameObject.GetComponent <enemy>();
         if (state == State.falling)
         {
             enemy.JumpedOn();
             Jumpin();
         }
         else
         {
             state = State.hurt;
             if (other.gameObject.transform.position.x > this.transform.position.x)
             {
                 //Enemy is to my right, should be damaged and move left
                 rb.velocity = new Vector2(-hurtForce, rb.velocity.y);
             }
             else
             {
                 //Enemy is to my left, should be damaged and move right
                 rb.velocity = new Vector2(hurtForce, rb.velocity.y);
             }
         }
     }
 }