Game Development with C# (Optional)
C# is widely used in game development with Unity, a popular game engine that allows building 2D and 3D games for multiple platforms.
1. Unity with C#
- Unity uses C# scripts for game logic.
- Projects include:
- Assets → Scripts, sprites, models, sounds
- Scenes → Game levels or screens
- GameObjects → Everything visible or interactive in the game
Setup:
- Download Unity Hub → Install latest Unity Editor.
- Create a 3D or 2D project → Start scripting in C#.
2. Scripts & Components
- Scripts are C# classes derived from
MonoBehaviour. - Components define behaviors for GameObjects.
Example: Simple Player Movement
- Attach script to a Player GameObject to control movement.
3. Game Objects
- GameObjects are everything in the scene: characters, cameras, lights, props.
- Components add functionality (e.g., Rigidbody for physics, Collider for collision).
4. Animations
- Unity supports 2D and 3D animations.
- Use Animator component and Animation Clips.
Example: Triggering animation
- Set triggers or bools in the Animator window to control animation states.
5. Event Handling in Games
- Handle input events, collisions, and triggers.
Collision Example
Trigger Example
Input Example
Summary of Chapter 17:
- Unity + C#: Powerful combination for game development.
- Scripts & Components: Control GameObject behavior with C# scripts.
- Game Objects: Everything in the scene is a GameObject with components.
- Animations: Control animations via Animator component and triggers.
- Event Handling: Respond to input, collisions, and triggers in real-time.