Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Learn UNITY & C# Basics with making simple 2d game


Learn UNITY & C# Basics with making simple 2d game

Learn UNITY & C# with making simple 2d game for mobile and pc, In this course you will learn about how to code a game in unity as well as ...

Enroll Now

Unity is one of the most popular game development engines available today. It's versatile, supports 2D and 3D game development, and is used by both indie developers and large game studios. Unity uses C# (pronounced C-sharp) as its primary programming language. In this guide, we'll walk through the basics of Unity and C# by creating a simple 2D game. This will provide a hands-on way to understand key concepts and get started with game development.

Setting Up Your Environment

  1. Install Unity: Go to the Unity website, download the Unity Hub, and install the latest version of Unity Editor.
  2. Install Visual Studio: Unity uses Visual Studio for C# scripting. Download and install Visual Studio Community, ensuring you include the Game development with Unity workload.

Starting a New Project

  1. Open Unity Hub: Click on "New" to create a new project.
  2. Select 2D Template: Name your project (e.g., "Simple2DGame"), choose a location, and select the 2D template. Click "Create".

Understanding the Unity Interface

When Unity opens your new project, you'll see several panels:

  • Scene View: This is where you build and arrange your game objects.
  • Game View: This shows what the player will see.
  • Hierarchy: Lists all game objects in the current scene.
  • Inspector: Shows properties of the selected game object.
  • Project: Contains all assets for your project.

Creating Your First Game Object

  1. Create a Player Object:

    • Right-click in the Hierarchy and select "Create Empty". Name it "Player".
    • With the Player object selected, click "Add Component" in the Inspector and add a "Sprite Renderer" component. This will render the player's sprite.
  2. Add a Sprite:

    • Download a simple player sprite (e.g., a small square) and save it in the "Assets" folder.
    • Drag the sprite into the Sprite Renderer component of the Player object.

Writing Your First Script

  1. Create a C# Script:

    • Right-click in the Project window, select "Create" -> "C# Script". Name it "PlayerController".
    • Drag this script onto the Player object in the Hierarchy to attach it.
  2. Open the Script in Visual Studio:

    • Double-click the "PlayerController" script to open it in Visual Studio. You'll see a basic class structure.
  3. Modify the Script:

    • Update the script to allow basic player movement:
      csharp
      using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 5.0f; void Update() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector2 movement = new Vector2(moveHorizontal, moveVertical); transform.Translate(movement * speed * Time.deltaTime); } }
    • Save the script and return to Unity.

Testing Player Movement

  1. Play the Game:
    • Click the "Play" button in Unity. Use the arrow keys to move the player object around the screen.

Adding a Background

  1. Create a Background:

    • Right-click in the Hierarchy, select "Create Empty" and name it "Background".
    • Add a "Sprite Renderer" component.
    • Import a background image into the "Assets" folder and assign it to the Sprite Renderer component of the Background object.
  2. Scale the Background:

    • Adjust the scale of the Background object to fit the camera view. You can do this by modifying the "Transform" properties in the Inspector.

Creating Enemy Objects

  1. Create an Enemy Prefab:

    • Right-click in the Hierarchy, select "Create Empty" and name it "Enemy".
    • Add a Sprite Renderer component and assign a sprite to it.
    • Add a Box Collider 2D component to detect collisions.
    • Create a new C# script named "EnemyController" and attach it to the Enemy object.
  2. Write Enemy Behavior:

    • Open the "EnemyController" script and add simple movement logic:
      csharp
      using UnityEngine; public class EnemyController : MonoBehaviour { public float speed = 2.0f; private Vector2 direction = Vector2.left; void Update() { transform.Translate(direction * speed * Time.deltaTime); } void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.CompareTag("Player")) { // Handle collision with player } } }
    • Save the script and return to Unity.
  3. Create Enemy Spawner:

    • Right-click in the Hierarchy, select "Create Empty" and name it "EnemySpawner".
    • Create a new C# script named "EnemySpawner" and attach it to the EnemySpawner object.
    • Write the following script to spawn enemies:
      csharp
      using UnityEngine; public class EnemySpawner : MonoBehaviour { public GameObject enemyPrefab; public float spawnRate = 2.0f; void Start() { InvokeRepeating("SpawnEnemy", 0.0f, spawnRate); } void SpawnEnemy() { float spawnY = Random.Range(-4.0f, 4.0f); Vector2 spawnPosition = new Vector2(10.0f, spawnY); Instantiate(enemyPrefab, spawnPosition, Quaternion.identity); } }
    • Save the script and return to Unity.
  4. Create Prefab and Assign to Spawner:

    • Drag the Enemy object from the Hierarchy to the Project window to create a prefab.
    • Delete the Enemy object from the Hierarchy.
    • Select the EnemySpawner object and assign the Enemy prefab to the "Enemy Prefab" field in the Inspector.

Adding Collisions and Game Over Logic

  1. Handle Player Collisions:

    • Modify the "PlayerController" script to detect collisions:
      csharp
      void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Enemy")) { // Handle game over logic } }
  2. Update Enemy Collision Logic:

    • Update the "EnemyController" script to handle player collisions:
      csharp
      void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.CompareTag("Player")) { Destroy(other.gameObject); // Handle game over logic } }

Polishing the Game

  1. Add UI Elements:

    • Create a UI Canvas for score and game over messages.
    • Add Text components to display the score and game over message.
  2. Implement Scoring System:

    • Create a new script named "GameController" and manage the score:
      csharp
      using UnityEngine; using UnityEngine.UI; public class GameController : MonoBehaviour { public Text scoreText; public GameObject gameOverPanel; private int score = 0; void Start() { UpdateScore(); } public void AddScore(int value) { score += value; UpdateScore(); } void UpdateScore() { scoreText.text = "Score: " + score.ToString(); } public void GameOver() { gameOverPanel.SetActive(true); } }
  3. Integrate Score and Game Over:

    • Update the player and enemy scripts to interact with the GameController:
      csharp
      // In PlayerController void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Enemy")) { FindObjectOfType<GameController>().GameOver(); } } // In EnemyController void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.CompareTag("Player")) { Destroy(other.gameObject); FindObjectOfType<GameController>().GameOver(); } }

Conclusion

Congratulations! You've just built a simple 2D game using Unity and C#. Through this process, you've learned how to set up a project, create and manipulate game objects, write scripts to control behavior, handle collisions, and manage game logic. Unity and C# offer a powerful combination for game development, and with these basics, you're ready to explore more advanced features and create more complex games. Keep experimenting and building, and soon you'll be making games that are even more sophisticated and engaging. Happy coding!