示例#1
0
 void OnCollisionEnter2D(Collision2D col)
 {
     //If the ball collides with the player paddle
     if (col.collider.gameObject.tag == "Ball")
     {
         if (vel.y != 0)                                                  //If the player's velocity is not zero
         {
             GameObject  ball        = col.collider.gameObject;           //Create a reference to the ball
             BallManager ballManager = ball.GetComponent <BallManager>(); //Create a reference to the ball manager component of the ball
             Vector2     ballVel     = ballManager.GetVelocity();         //Get the ball's velocity
             if (vel.y > 0)
             {
                 //If the player's velocity is greater than zero, set the ball's y velocity to it's absolute (positive) value
                 ballManager.SetVelocity(new Vector2(ballVel.x, Mathf.Abs(ballVel.y)));
             }
             else
             {
                 //If the player's velocity is less than zero, set the ball's y velocity to it's negative value
                 ballManager.SetVelocity(new Vector2(ballVel.x, Mathf.Abs(ballVel.y) * -1f));
             }
         }
     }
 }