Create Simple 2D Game in Unity

Create Simple 2D Game in Unity

Here is an example of how you could create a simple 2D game in Unity using scripts:

  1. First, create a new Unity project and set the project up for 2D by going to "Edit > Project Settings > Player" and setting the "Default orientation" to "Portrait".

  2. Next, create a new scene by going to "File > New Scene".

  3. In the Hierarchy panel, create an empty GameObject by right-clicking the Hierarchy and selecting "Create Empty". Rename this GameObject to "GameController".

  4. Next, create a new script by right-clicking the "GameController" GameObject in the Hierarchy and selecting "Create > C# Script". Name the script "GameController".

  5. Double-click the "GameController" script to open it in your code editor. Add the following code to the script:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Game started!");
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}


This script will be the main controller for our game. The "Start" function will be called when the game starts, and the "Update" function will be called every frame.

  1. In the Hierarchy panel, create a new sprite by right-clicking the Hierarchy and selecting "2D Object > Sprite". Rename this GameObject to "Player".

  2. In the "Inspector" panel, click the "Sprite" field and select a sprite for the player.

  3. Next, create a new script for the player by right-clicking the "Player" GameObject in the Hierarchy and selecting "Create > C# Script". Name the script "PlayerController".

  4. Double-click the "PlayerController" script to open it in your code editor. Add the following code to the script:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        transform.position = transform.position + new Vector3(horizontalInput, 0, 0);
    }
}


This script will control the player's movement. The "Update" function gets the horizontal input from the player (using the left and right arrow keys or the "A" and "D" keys) and moves the player left or right.

That's it! You now have a simple 2D game in Unity with scripts controlling the game and the player. You can add more gameplay elements, such as enemies and obstacles, by creating additional GameObjects and scripts.