How to Use Unity: 2D/3D Games, C# Scripting, and Mobile Deployment
Key Takeaways
- Unity uses a component-based system: attach scripts, physics, and visuals to GameObjects.
- C# scripts control behavior via methods like `Start()` and `Update()`. You write them in Visual Studio or VS Code.
- For 2D games, use `SpriteRenderer` and `Rigidbody2D`; for 3D, `MeshRenderer` and `Rigidbody`.
- Mobile deployment requires the Build Settings panel and platform-specific tweaks (e.g., Android SDK, iOS provisioning).
Getting Started with Unity
Unity is the engine behind games like *Hollow Knight*, *Cities: Skylines*, and *Pokémon GO*. It’s free for individuals and small teams (you pay royalties only if you earn over $200K in 12 months). I’ve used it for five years, and the biggest mistake beginners make is jumping into 3D without understanding the scene hierarchy.
Install and Create a Project
Download Unity Hub from unity.com. Install a version (I recommend 2022.3 LTS for stability). Open Hub, click New Project, and choose either 2D Core or 3D Core. Name it something like "MyFirstGame" — no spaces, please. Unity creates a scene with a default camera and directional light (for 3D).
The Unity Interface
Your workspace has five key panels:
- Scene: Where you arrange objects visually.
- Game: Shows what the player sees.
- Hierarchy: Lists every object in the scene.
- Inspector: Shows properties of the selected object.
- Project: Holds all your assets (scripts, textures, models).
Building a 2D Game: Sprite and Movement
Let’s make a simple 2D character that moves with arrow keys.
1. Create a Game Object: Right-click in Hierarchy → 2D Object → Sprite. Rename it "Player".
2. Add a Sprite: In the Inspector, click the circle next to Sprite and pick a default square (or import your own PNG).
3. Attach Physics: Click Add Component → Rigidbody2D. Set Gravity Scale to 0 (so it doesn’t fall).
4. Write a Script: Create a C# script in the Project panel (right-click → Create → C# Script). Name it `PlayerMovement`. Double-click to open it in Visual Studio (install Unity’s workload if prompted).
Here’s the code:
```csharp
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f;
void Update()
{
float moveX = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(moveX, 0);
transform.Translate(movement * speed * Time.deltaTime);
}
}
```
- `Input.GetAxis` returns -1 (left), 0, or 1 (right).
- `Time.deltaTime` makes movement frame-rate independent.
Drag the script from Project onto the Player object in Hierarchy. Press Play — your square moves.
3D Game: Camera, Lighting, and Scripts
For 3D, the workflow is similar but with extra dimensions.
1. Create a 3D Object: Right-click in Hierarchy → 3D Object → Cube. Position it at (0, 0, 0).
2. Add a Rigidbody: Component → Physics → Rigidbody. This enables gravity and collisions.
3. Control with a Script: Create a script called `PlayerController`. Use `FixedUpdate` for physics:
```csharp
void FixedUpdate()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
GetComponent
}
```
Comparison: 2D vs 3D Physics
| Feature | 2D | 3D |
| --------- | ---- | ---- |
| Physics Component | `Rigidbody2D` | `Rigidbody` |
| Collider Type | `BoxCollider2D`, `CircleCollider2D` | `BoxCollider`, `SphereCollider` |
| Movement Units | `transform.Translate` (x,y) | `Rigidbody.AddForce` (x,y,z) |
| Gravity | Set in `Rigidbody2D` | Default 9.81 m/s² |
For 3D, always use `FixedUpdate` for physics changes to avoid jittery movement.
C# Scripting: Core Patterns
Unity scripts follow a predictable lifecycle:
- `Awake()` runs when the object loads (good for references).
- `Start()` runs before the first frame (good for initialization).
- `Update()` runs every frame (good for input).
- `FixedUpdate()` runs at fixed intervals (good for physics).
Pro tip: Cache components in `Awake` to avoid `GetComponent` in `Update`, which is slow. Example:
```csharp
private Rigidbody rb;
void Awake()
{
rb = GetComponent
}
void FixedUpdate()
{
rb.AddForce(...);
}
```
Common Beginner Mistakes
- Forgetting to attach the script to a GameObject.
- Using `Update` for physics (use `FixedUpdate`).
- Not using `Time.deltaTime` (movement will be frame-dependent).
Mobile Deployment: Android and iOS
Unity makes mobile deployment straightforward, but the setup is fiddly.
For Android
1. Install Android SDK via Unity Hub (under Add Modules when installing Unity).
2. Open File → Build Settings. Switch platform to Android (click Switch Platform).
3. In Player Settings (bottom left), set Package Name (e.g., `com.yourname.game`).
4. Connect your Android phone via USB, enable Developer Options, and click Build and Run. Unity generates an `.apk` file.
For iOS (macOS only)
1. Install Xcode from the App Store.
2. In Build Settings, switch to iOS.
3. Build — Unity creates an Xcode project.
4. Open that project in Xcode, connect your iPhone, and hit Run.
Real numbers: A simple 2D game builds to about 15–30 MB on Android. 3D games with textures can hit 100 MB. Use Texture Compression in Player Settings to reduce size.
Mobile-Specific Tweaks
- Use Mobile Input (touch controls) instead of mouse/keyboard. Unity has a `Touch` class.
- Adjust Resolution Scaling in Player Settings to target common resolutions (e.g., 1080x1920 for phones).
- Test on a real device — the editor simulates about 50% of real performance.
Troubleshooting Common Issues
- Script not working: Check the Console (Ctrl+Shift+C) for errors. Missing semicolons are the #1 cause.
- Physics objects falling through floor: Make sure the floor has a `BoxCollider` (or `BoxCollider2D`).
- Mobile build fails: Update your SDK/NDK paths in Preferences → External Tools.
FAQ
Q: Do I need to know programming to use Unity?
A: Yes, for any non-trivial game. You need basic C#: variables, if-statements, and functions. Unity’s Visual Scripting (formerly Bolt) can help, but it’s limited. I recommend learning C# fundamentals first — Codecademy’s free course takes about 10 hours.
Q: Can I make a 3D game without modeling skills?
A: Yes. Use free assets from the Unity Asset Store (e.g., “Starter Assets” by Unity). For prototypes, use Unity’s built-in primitives (cubes, spheres). I made my first 3D game entirely with cubes and free scripts.
Q: How do I publish to the App Store or Google Play?
A: For Google Play, you need a developer account ($25 one-time fee). For iOS, an Apple Developer account ($99/year). Both require signing the build with a certificate — Unity’s documentation covers the steps. Expect the first submission to take 2–3 days for review.