Create Simple 2D Game for beginners in Unity

 Create Simple 2D Game for beginners in Unity

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

  1. Create a new Unity project and set the project up for 2D games by going to Edit > Project Settings > Player and then setting the "Default Orientation" to "Portrait" and the "Graphics APIs" to "Direct3D 11".

  2. Create a new scene by going to File > New Scene.

  3. Create a new empty game object by going to GameObject > Create Empty. This will be the root game object for the scene.

  4. Create a new sprite by going to GameObject > 2D Object > Sprite. This will be the player character.

  5. Add a Rigidbody2D component to the player sprite by going to Add Component > Physics 2D > Rigidbody2D in the Inspector panel. This will allow the player sprite to be affected by physics.

  6. Add a Box Collider 2D component to the player sprite by going to Add Component > Physics 2D > Box Collider 2D in the Inspector panel. This will allow the player sprite to interact with other colliders in the scene.

  7. Create a new script by going to Assets > Create > C# Script. Name the script "PlayerController".

  8. Double-click the 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
{
    public float speed = 5.0f;
    private Rigidbody2D rigidbody2D;

    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector2 movement = new Vector2(horizontalInput, verticalInput);
        rigidbody2D.velocity = movement * speed;
    }
}



  1. Attach the script to the player sprite by dragging and dropping the script onto the sprite in the Hierarchy panel.

  2. Test the game by pressing the play button in the top center of the Unity editor. The player sprite should move around the screen when you use the arrow keys on your keyboard.

That's it! This is a very basic example of how you can create a 2D game in Unity using scripts. You can add more complexity to the game by adding more sprites and scripts, as well as implementing features like enemy AI, power-ups, and so on.