Unity Guide — Game Engine Tutorial with C# Examples
Unity is the world’s most popular game engine, powering over 50% of all mobile games and thousands of PC and console titles. It combines a visual editor with C# scripting to let you build 2D, 3D, VR, and AR games for 25+ platforms from a single project.
What You’ll Learn
You’ll master Unity’s core concepts — scenes, GameObjects, components, prefabs, physics, and C# scripting — and build a player movement script with character controller physics.
Why Unity Matters
Unity powers games like Hollow Knight, Cities: Skylines, Pokémon GO, and Among Us. Beyond games, it’s used in architecture visualization, film production, automotive simulations, and training systems. At DodaTech, we use Unity for interactive product demos in Doda Browser that showcase hardware acceleration across devices.
Unity Learning Path
flowchart LR
A[Game Dev Overview] --> B[Unity Guide]
B --> C{You Are Here}
C --> D[Unreal Engine Guide]
C --> E[Blender Basics]
style C fill:#f90,color:#fff
Core Architecture: Scenes, GameObjects, and Components
Every Unity project is built from three fundamental concepts:
Scenes are containers for your game world — a menu scene, a gameplay level, a game-over screen. Each scene is like a separate room in your game.
GameObjects are every object in a scene — characters, cameras, lights, empty positional markers. They’re invisible containers that hold components.
Components give GameObjects their behavior. A C# script, a mesh renderer, a collider, a rigid body — each is a component attached to a GameObject.
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f;
public float jumpForce = 7f;
private Rigidbody rb;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
Vector3 move = transform.right * moveX + transform.forward * moveZ;
rb.MovePosition(transform.position + move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
isGrounded = true;
}
}Expected behavior: The player moves with WASD keys, jumps with Space, and can only jump when touching the ground. Time.deltaTime makes movement frame-rate independent.
Prefabs: Reusable GameObjects
Prefabs are templates. Create a GameObject once — an enemy, a bullet, a pickup — save it as a prefab, and instantiate it at runtime:
public GameObject bulletPrefab;
public Transform firePoint;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}Changes to the prefab asset update every instance. This is how Unity achieves efficient content pipelines — the same approach used in DodaZIP for template-based file generation.
Physics System
Unity’s physics engine (PhysX) handles realistic movement, collisions, and forces:
| Component | Purpose |
|---|---|
| Rigidbody | Enables physics (gravity, forces, velocity) |
| Collider | Defines shape for collisions (Box, Sphere, Capsule, Mesh) |
| Joint | Connects rigidbodies (Hinge, Spring, Fixed) |
| Physic Material | Controls bounciness and friction |
// Adding force to launch an object
void Launch()
{
rb.AddForce(transform.forward * 20f, ForceMode.Impulse);
}Common Mistakes Beginners Make
1. Modifying transform.position on a Rigidbody
Use rb.MovePosition() or rb.AddForce() instead. Direct position changes bypass physics and cause jittery movement.
2. Forgetting Time.deltaTime
Movement without Time.deltaTime runs at different speeds on different frame rates. Always multiply movement by Time.deltaTime for consistency.
3. Checking Input in FixedUpdate
Input.GetAxis() reads correctly in Update(). In FixedUpdate() (physics loop), input values may be stale. Read input in Update() and apply physics in FixedUpdate().
4. Scaling the Parent Instead of Children
Non-uniform scaling on a parent object distorts child colliders. Keep root objects at (1,1,1) scale and use child objects for visual scaling.
5. Not Using CompareTag()
gameObject.tag == "Enemy" allocates memory (string comparison). Use gameObject.CompareTag("Enemy") — it’s faster and zero-alloc.
6. Attaching Scripts to Wrong Objects
A movement script on the camera won’t move the player. Attach scripts to the GameObject they control.
7. Forgetting to Set Layer Collision Matrix
Uncheck unnecessary layer pairs in Edit > Project Settings > Physics. This prevents bullets hitting the player who fired them.
Practice Questions
1. What’s the difference between Update() and FixedUpdate()?
Update() runs every frame (variable rate). FixedUpdate() runs at a fixed timestep (default 0.02s) and is used for physics calculations.
2. Why use Instantiate() with prefabs?
Prefabs allow you to spawn objects at runtime without having copies sitting in the scene. Changes to the prefab propagate to all instances.
3. What does Time.deltaTime do?
It returns the time in seconds since the last frame. Multiplying movement by it makes speed independent of frame rate.
4. How do you detect a collision in Unity?
Add OnCollisionEnter(Collision collision) to a script on a GameObject that has both a Collider and a Rigidbody.
5. Challenge: Add a sprint mechanic.
Track a isSprinting bool. When LeftShift is held, multiply speed by 2. Add a stamina bar that depletes while sprinting and recovers when walking.
Mini Project: Rolling Ball Game
Create a new 3D project and build a game where a ball collects pickups:
- Add a Sphere with Rigidbody and
SphereCollider - Write a script that applies force based on arrow keys
- Create Cube pickups with
BoxCollider(isTrigger = true) - Add
OnTriggerEnterto detect pickups, destroy them, and increment score - Display score with
OnGUI()or aTextMeshProUI element
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Pickup"))
{
Destroy(other.gameObject);
score++;
scoreText.text = "Score: " + score;
}
}FAQ
What’s Next
Congratulations on completing this Unity tutorial! Here’s where to go from here:
- Practice daily — Build one small mechanic per day
- Build a project — Create a simple 3D platformer
- Explore related topics — 3D modeling in Blender, advanced physics
- Join the community — Share your progress and learn from others
Remember: every expert was once a beginner. Keep coding!
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro