コード例 #1
0
    void OnCollisionEnter2D(Collision2D collision)
    {
        Goomba goomba = collision.collider.GetComponent <Goomba>();

        if (goomba != null)
        {
            // Fireball has kicked a goomba
            goomba.Hurt();
            ExplodeImmediately();
        }
        else
        {
            Vector2 normal     = collision.contacts[0].normal;
            Vector2 leftSide   = new Vector2(-1f, 0f);
            Vector2 rightSide  = new Vector2(1f, 0f);
            Vector2 bottomSide = new Vector2(0f, 1f);

            if (normal == leftSide || normal == rightSide)
            { // explode if side hit
                Explode();
            }
            else if (normal == bottomSide)
            { // bounce off
                rigidbody.velocity = new Vector2(rigidbody.velocity.x, absVelocity.y);
            }
            else
            {
                rigidbody.velocity = new Vector2(rigidbody.velocity.x, -absVelocity.y);
            }
        }
    }
コード例 #2
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Goomba goomba = collision.collider.GetComponent <Goomba>();

        if (goomba != null)
        {
            foreach (ContactPoint2D point in collision.contacts)
            {
                Debug.Log(point.normal);
                Debug.DrawLine(point.point, point.point + point.normal, Color.red, 10);
                if (point.normal.y >= 0.6f)
                {
                    Vector2 velocity = rb.velocity;
                    velocity.y  = jumpSpeed;
                    rb.velocity = velocity;
                    goomba.Hurt();
                }
                else
                {
                    Hurt();
                    break;
                }
            }
        }
    }