示例#1
0
        void FixedUpdate()
        {
            var ball = PongGame.BoundsFromTransform(transform);
            var p    = transform.position + _velocity * Time.fixedDeltaTime;

            p.x -= ball.size.x / 2;
            p.y -= ball.size.y / 2;

            //Check vs game borders
            if (ball.max.y > _game.Border.max.y)          //Top
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.down);
                SetTransformY(_game.Border.max.y - ball.size.y / 2);
            }
            else if (ball.min.y < _game.Border.min.y)     //Bot
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.up);
                SetTransformY(_game.Border.min.y + ball.size.y / 2);
            }

            var p1 = PongGame.BoundsFromTransform(Player1.transform);
            var p2 = PongGame.BoundsFromTransform(Player2.transform);

            if (ball.Intersects(p1))   //Player 1 controller
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.right).normalized * ++_speed;
                SetTransformX(p1.max.x + ball.size.x / 2);
                Player1.Hits++;
                Debug.Log(Player1.Hits);
            }
            else if (ball.min.x < _game.Border.min.x)    //Player 1 Goal
            {
                _game.Score(Player.Player2);
                //Reset(1);
            }

            if (ball.Intersects(p2))   //Player 2 controller
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.left).normalized * ++_speed;
                SetTransformX(p2.min.x - ball.size.x / 2);
                Player2.Hits++;
            }
            else if (ball.max.x > _game.Border.max.x)    //Player 2 Goal
            {
                _game.Score(Player.Player1);
                //Reset(-1);
            }

            //Update position
            transform.position += _velocity * Time.fixedDeltaTime;
            PongGame.DebugDrawBounds(PongGame.BoundsFromTransform(transform), Color.red);
        }