예제 #1
0
 private void killSnake()
 {
     print("snake dies");
     // lost = true;
     if (test_mode == false)
     {
         CancelInvoke();
     }
     game_scr.game_hit("snake");
 }
예제 #2
0
    void OnCollisionEnter2D(Collision2D col)
    {
        // Note: 'col' holds the collision information. If the
        // Ball collided with a racket, then:
        //   col.gameObject is the racket
        //   col.transform.position is the racket's position
        //   col.collider is the racket's collider

        print("Ball: " + col.gameObject.name);

        // Hit the left Racket?
        if (col.gameObject.name == "RacketLeft")
        {
            // Calculate hit Factor
            float y = hitFactor(transform.position,
                                col.transform.position,
                                col.collider.bounds.size.y);

            // Calculate direction, make length=1 via .normalized
            Vector2 dir = new Vector2(1, y).normalized;

            // Set Velocity with dir * speed
            speed = speed + 2;
            GetComponent <Rigidbody2D>().velocity = dir * speed;
        }

        // Hit the right Racket?
        if (col.gameObject.name == "RacketRight")
        {
            // Calculate hit Factor
            float y = hitFactor(transform.position,
                                col.transform.position,
                                col.collider.bounds.size.y);

            // Calculate direction, make length=1 via .normalized
            Vector2 dir = new Vector2(-1, y).normalized;
            speed = speed + 2;
            // Set Velocity with dir * speed
            GetComponent <Rigidbody2D>().velocity = dir * speed;
        }

        if (col.gameObject.name.StartsWith("Head"))
        {
            print("ball hits head");
            snk_scpt            = snk_obj.GetComponent <Snake>();
            snk_scpt.hitBy_type = ball_type;
        }

        if (col.gameObject.name.StartsWith("TailPrefab"))
        {
            print("ball hits tail");
            snk_scpt            = snk_obj.GetComponent <Snake>();
            snk_scpt.hitBy_type = ball_type;
        }

        if (col.gameObject.name.StartsWith("BorderLeft"))
        {
            print("ball hits BorderLeft");
            if (test_mode == false)
            {
                GetComponent <Rigidbody2D>().velocity = Vector2.right * 0;
            }

            game_scrt.game_hit("ping");
        }

        if (col.gameObject.name.StartsWith("BorderRight"))
        {
            print("ball hits BorderRight");
            if (test_mode == false)
            {
                GetComponent <Rigidbody2D>().velocity = Vector2.right * 0;
            }
            game_scrt.game_hit("pong");
        }
    }