Posted on

How to make Pong in Unity Lesson 1

Play Classic Pong

Welcome to the Info Gamer tutorial series and ebook on how to make your very own Pong Clone using Unity. In this ebook, we will take you step by step through all the key mechanics need to build the basic game of Pong. Along the way, we will explain the purpose of each step and what is possible so you can better understand and learn how to make video games.

If you are not familiar with Pong and would to play it first you can play our version here

Preparation

Things you will need

Before we begin there are a few things you will need. 

  • Computer
  • Internet connection
  • Unity Hub
  • Unity editor

If you are viewing this now then we can assume you already have a computer and an internet connection so you are already halfway there. I also assume you already have Unity Hub and a version of the Unity editor installed but if you don’t you can find our tutorial series on how to install Unity here

The version of Unity does not matter.

Once you have everything you need you will then need to create a new Unity project. You will want to use the 2D template. If you need help knowing how to create a new project follow this tutorial here






Once you have these images imported into your project we can then begin designing our scene. We have divided the game scene up into the following objects. (Click the links to see Inspector)

  • : Your empty scene should already have a camera game object called Main Camera. Make sure this object has the following inspector
  • Directional Light: This is another object that should already be in your empty scene. You don’t need to do anything with this object, in fact, you can just delete this object as we will only be using unlit sprites for this game.
  • Environment: This is a new empty object with the following children
    • : This is a sprite object with the black square sprite
    • : This is an empty object with a 2D box collider
    • LeftWall: This is the same as RightWall but with a negative X position.
    • : This is similar to RightWall and LeftWall but the size of the box collider and position are different.
    • EndTwo: This is the same as EndOne but the Y position is negative.
    • : This is a sprite object with the dotted line sprite.
  • : This is a UI canvas object with the following Canvas Scaler setting.
    • : Is a UI text object anchored to the bottom of the canvas with a 0 for the Text field.
    • ScoreTwo: This is the same a ScoreOne but it is anchored to the top of the canvas.
    • : This is another text object anchored to the bottom. This has the following string for the Text field. “< A           D >”
    • Player2Keys: This is the same as Player1Keys but this object is anchored to the top of the canvas and has the following string for its Text field. “< J           L >”
    • : This is a UI Panel object that has its image transparency turned down to zero.
      • : This is a UI Text object used for displaying the winner. The following string is the value of the Text field. “Player _ Wins”
      • : This is a UI Button object.
        • : This is the UI Text object that comes with the default UI Button.
    • EventSystem: This is the Event System object that is created by default when you create a UI Canvas

Note: Do not forget the tags

Here is what your hierarchy should look like.

Here is what your Game View should look like.

Note: Once you have all this created you can then disable the GameOverPanel in your hierarchy.

At this point, you should have the environment for your game scene complete.

Posted on

How to Make ZigZag in Unity Lesson 10

This is the final lesson for the basics of creating a ZigZag game in Unity. For this lesson, I only show you what kind of changes I have made to my version of the game to make it more my own. This should give you some ideas of what is possible and what you can do to create your own version.

Posted on

How to Make ZigZag in Unity Lesson 9

For this ZigZag lesson, I will teach you how to make the ball object speed up based on the player’s score. This will make it so the game becomes more and more challenging over time which is important as you want your game to become challenging so the players will be tested, loose and want to try again.

Unlock Code and Member Content

MenuController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MenuController : MonoBehaviour
{
    public static MenuController instance;
    [SerializeField] GameObject gameOverPanel;
    [SerializeField] GameObject mainPanel;
    [SerializeField] int gameLevelNumber;

    int myScore = -1;

    [SerializeField] Text scoreDisplay1;
    [SerializeField] Text scoreDisplay2;

    [SerializeField] float speedInterval;

    private void OnEnable()
    {
        instance = this;
    }

    public void GameOver()
    {
        gameOverPanel.SetActive(true);
        mainPanel.SetActive(false);
    }

    public void Restart()
    {
        SceneManager.LoadScene(gameLevelNumber);
    }

    public void TurnButton()
    {
        if(!BallController.instance.isStarted)
        {
            BallController.instance.isStarted = true;
        }
        BallController.instance.isGoingRight = !BallController.instance.isGoingRight;
        AddScore();
    }

    void AddScore()
    {
        myScore++;
        scoreDisplay1.text = myScore.ToString();
        scoreDisplay2.text = myScore.ToString();

        SpeedUp();
    }

    void SpeedUp()
    {
        BallController.instance.currentSpeed += speedInterval;
    }
}
using Photon.Chat;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PhotonChatManager : MonoBehaviour, IChatClientListener
{
    #region Setup

    [SerializeField] GameObject joinChatButton;
    ChatClient chatClient;
    bool isConnected;
    [SerializeField] string username;

    public void UsernameOnValueChange(string valueIn)
    {
        username = valueIn;
    }

    public void ChatConnectOnClick()
    {
        isConnected = true;
        chatClient = new ChatClient(this);
        //chatClient.ChatRegion = "US";
        chatClient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat, PhotonNetwork.AppVersion, new AuthenticationValues(username));
        Debug.Log("Connenting");
    }

    #endregion Setup

    #region General

    [SerializeField] GameObject chatPanel;
    string privateReceiver = "";
    string currentChat;
    [SerializeField] InputField chatField;
    [SerializeField] Text chatDisplay;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (isConnected)
        {
            chatClient.Service();
        }

        if (chatField.text != "" &amp;&amp; Input.GetKey(KeyCode.Return))
        {
            SubmitPublicChatOnClick();
            SubmitPrivateChatOnClick();
        }
    }

    #endregion General

    #region PublicChat

    public void SubmitPublicChatOnClick()
    {
        if (privateReceiver == "")
        {
            chatClient.PublishMessage("RegionChannel", currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    public void TypeChatOnValueChange(string valueIn)
    {
        currentChat = valueIn;
    }

    #endregion PublicChat

    #region PrivateChat

    public void ReceiverOnValueChange(string valueIn)
    {
        privateReceiver = valueIn;
    }

    public void SubmitPrivateChatOnClick()
    {
        if (privateReceiver != "")
        {
            chatClient.SendPrivateMessage(privateReceiver, currentChat);
            chatField.text = "";
            currentChat = "";
        }
    }

    #endregion PrivateChat

    #region Callbacks

    public void DebugReturn(DebugLevel level, string message)
    {
        //throw new System.NotImplementedException();
    }

    public void OnChatStateChange(ChatState state)
    {
        if(state == ChatState.Uninitialized)
        {
            isConnected = false;
            joinChatButton.SetActive(true);
            chatPanel.SetActive(false);
        }
    }

    public void OnConnected()
    {
        Debug.Log("Connected");
        joinChatButton.SetActive(false);
        chatClient.Subscribe(new string[] { "RegionChannel" });
    }

    public void OnDisconnected()
    {
        isConnected = false;
        joinChatButton.SetActive(true);
        chatPanel.SetActive(false);
    }

    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
        string msgs = "";
        for (int i = 0; i &lt; senders.Length; i++)
        {
            msgs = string.Format("{0}: {1}", senders[i], messages[i]);

            chatDisplay.text += "\n" + msgs;

            Debug.Log(msgs);
        }

    }

    public void OnPrivateMessage(string sender, object message, string channelName)
    {
        string msgs = "";

        msgs = string.Format("(Private) {0}: {1}", sender, message);

        chatDisplay.text += "\n " + msgs;

        Debug.Log(msgs);
        
    }

    public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
    {
        throw new System.NotImplementedException();
    }

    public void OnSubscribed(string[] channels, bool[] results)
    {
        chatPanel.SetActive(true);
    }

    public void OnUnsubscribed(string[] channels)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserSubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    public void OnUserUnsubscribed(string channel, string user)
    {
        throw new System.NotImplementedException();
    }

    #endregion Callbacks
}