ACTIVITY 2 AND 3


 PASS ME THE BALL

A 2D UNITY GAME

THE CONCEPT
 The right player scores a point if the ball hits the left wall. The left player scores a point if the ball hits the right wall. If it hits the top or bottom wall then it bounces off. Each player will have a racket that can be moved up and down to hit back the ball.

KEYS
FIRST PLAYER: W - UP                                    SECOND PLAYER: ↑ - UP
                          S  - DOWN                                                            ↓ - DOWN                       


STEPS IN MAKING IN UNITY

Let's add four walls to our game. All we need to make a wall is a so called Sprite, or in other words: a Texture.We will use one horizontal Sprite for the top and bottom walls and one vertical Sprite for the left and right walls Let's select both wall images and then take a look at the Inspector, where we will apply the following Import Settings:



drag the walls to the project area "twice" to create a solid wall. After dragging select all 4 in the hierarchy so we can add collider.click on the Add Component button in the Inspector and then start typing Box Collider 2D




 Let's add the dotted line in the middle and We will select it in the Project Area and then apply the same Import Settings.Afterwards we can drag it from the Project Area into the Scene.



Adding 
The Racket Texture, still apply the same import setting and drag in twice in the project area as we both need two players in this game.After dragging select all 4 in the hierarchy so we can add collider.click on the Add Component button in the Inspector and then start typing BoxCollider2D and RigidBody 2D  so our object will never move out of the wall




And now we will add a script for the racket movement.

using System.Collections;
using UnityEngine;

public class MoveRacket : MonoBehaviour
{  
    public float speed = 30;
    public string axis = "Vertical";
       
    void FixedUpdate()
    {
        float v = Input.GetAxisRaw(axis);
        GetComponent<Rigidbody2D>().velocity = new Vector2(0, v) * speed;
    }
}

Let's select Edit -> Project Settings. This will bring up a pop-up window with a variety of settings Here we can modify the current Vertical axis so that it only uses the W and S keys. We will also make it use only Joystick 1.


Afterwards we will select the RacketRight GameObject and change the MoveRacket Script's Axis property to Vertical2:

And now the Pingpong Ball. Just use the import setting that we use and drag it in the Project Area, Add BoxCollider2D, and now we will create 
Physics 2D Material which we will name BallMaterial  and put 1 in bounciness to modify Then we drag the material from the Project Area into the Material slot of the Ball's Collider,In order to make our ball move through the game world, we will add a Rigidbody2D


And the last part is adding script for the ball. Create a new C# Script and name it ball.

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour
{
    public float speed = 30;

    void Start()
    {
        // Initial Velocity
        GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
    }

    float hitFactor(Vector2 ballPos, Vector2 racketPos,
                float racketHeight)
    {
        // ascii art:
        // ||  1 <- at the top of the racket
        // ||
        // ||  0 <- at the middle of the racket
        // ||
        // || -1 <- at the bottom of the racket
        return (ballPos.y - racketPos.y) / racketHeight;
    }

    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

        // 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
            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;

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

If we press play, we can now influence the ball's bouncing direction depending on where we hit it with the Racket.




Comments