Pseudocode
Deslize para mostrar o menu
Introduction to Programming in Unity: Pseudocode
At this point, we have animations and physics set up - but our project still isn't a game.
The player can't move, the camera doesn't follow, platforms don't spawn, and there's no score.
To make things actually happen, we need to start writing our own code.
Scripts, GameObjects, and C#
In Unity, we write code by creating scripts and attaching them to GameObjects.
Each script is a component that tells a specific object how to behave.
This approach is called Object-Oriented Programming.
Unity uses C# as its programming language.
Creating a Script:
- Select the GameObject (for example, the Player);
- Click Add Component;
- Type a script name (no spaces - use capital letters instead, e.g. PlayerController);
- Click Create Script.
Unity creates the script automatically and opens it in Visual Studio when you double-click it.
What Is Pseudocode?
Before writing real C# code, it's helpful to first write pseudocode.
Pseudocode is:
- an informal set of instructions written in plain language;
- not real code (so it won't run);
- a way to plan what your script should do before worrying about how to write it in C#.
Think of it as a stepping stone between intention and implementation.
Writing Pseudocode in Unity
You can write pseudocode anywhere:
- on paper;
- in a text editor;
- inside Visual Studio as comments.
In C#, comments start with //.
Anything written after // is ignored by Unity and won't cause errors.
Example:
// Make the player move left and right when I press A and D
This is a start, but it's vague.
Making Pseudocode Better
Good pseudocode is:
- specific;
- unambiguous;
- step-by-step.
We improve pseudocode by asking:
- what input?;
- what object?;
- what component?;
- in what direction?;
- by how much?.
After refining, our player movement pseudocode becomes:
// If the player presses A:
// Make the player's rigidbody velocity on the x-axis equal to -movementSpeed
// If the player presses D:
// Make the player's rigidbody velocity on the x-axis equal to movementSpeed
This clearly explains:
- when something happens (input);
- what changes (velocity);
- where it changes (rigidbody, x-axis);
- by how much (movementSpeed).
Later, we'll translate this directly into C#.
Practice: Write Your Own Pseudocode
Try writing pseudocode for other game mechanics, such as:
- how the camera should follow the player;
- how the score increases as the player moves higher;
- how the game detects when the player falls and restarts.
Don't worry about getting it perfect.
Pseudocode is just a thinking tool - a way to organize your logic before coding.
In the next videos, we'll start converting this pseudocode into real C# scripts.
1. You want the player to move upward when the W key is pressed by adding velocity using the physics system. Which pseudocode best describes this behaviour?
2. You want the player’s score to increase when the player moves higher. Which pseudocode best describes this idea?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo