To be able to:
In your Start Menu, scroll down to 'U' and open the Unity Hub
In your Unity Hub you will need to click on New Project and give it the settings on the next slide.
Select a 2D template in the main section of your Hub, then name your Project "Testing Tools"
Once you have done both, select 'Create Project' in the bottom right corner.
Follow the instructions in the following slides to complete build your first game of Pong
Go to File, New Scene and make sure to press "Basic 2D (Built-in)" -->
(You don't need to save previous work)
Right click in the assets area (not over the folder), Select "Import new asset", select "square.png"
You can check the size fits by selecting Game at the top and look to see if your TopWall covers the entire width of the camera.
Our Player1Goal needs to be outside of the camera and cover the height of the camera.
(On the next slide)
Now that we have the boarders of the game, we need some players to control.
Change the Tag of your Player1 to Player.
With all objects selected, click on add component in the bottom right and type in Box Collider 2D
Changing this allows the score to change when the ball hits the goal
It is good practise to keep objects organised, so we will put the boarders into a folder
You have now got the base of your game and the next step is to add in some logic so your game works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour
{
public KeyCode up;
public KeyCode down;
Rigidbody2D rigidBody;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(up))
{
rigidBody.velocity = new Vector2(0f, 7f);
}
else if(Input.GetKey(down))
{
rigidBody.velocity = new Vector2(0f, -7f);
}
else
{
rigidBody.velocity = new Vector2(0f, 0f);
}
}
}
Copy and replace the code in the window that opens, with this code on the left.
KEEP TRACK OF THE CAPITAL LETTERS!
Press CTRL + S to save your code, otherwise you won't be able to control your players.
Select both of your players
Press play again when you are done
Copy and replace the code in the window that opens, with this code on the left.
KEEP TRACK OF THE CAPITAL LETTERS!
Press CTRL + S to save your code, otherwise your ball won't move around.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
Rigidbody2D rigidBody;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
Direction();
}
// Update is called once per frame
void Update()
{
}
public void Direction()
{
int direction;
direction = Random.Range(0, 2);
if(direction == 0)
{
rigidBody.velocity = new Vector2(5f, -3f);
}
else
{
rigidBody.velocity = new Vector2(-5f, 3f);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Goal : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collider)
{
collider.GetComponent<Ball>().Direction();
collider.transform.position = new Vector2(0f, 0f);
}
}
Add the function OnTriggerEnter2D() to your script.
Once you have, make sure to press CTRL + S to save your code.
Experiment with certain aspects of the physics to change the difficulty level of your games. You can try adapting: