Unity Engine Physics Components
One of the core foundations of most games, whether they be 2D or 3D, is the Physics involved in them. Physics need not be complicated stuff like wave equations and fluid dynamics. Even the most basic stuff that you do in making a game involves physics.
For example, moving the player generally involves either adding a force on the object that is the player, or displacing it by a certain amount. Making it float or drown in water, or just slide down slopes is also involved with how physics work.
Physics in Unity, and in general game design, are really important. Most of the time, it's the physics of a game which cause
"glitches". Some examples of these would be OoBs (Out of Bounds) and clipping errors.
In this section, we'll be looking at how we can add physical properties to objects in our game, and how we can make them work the way we want them too. We'll also learn more about referencing objects and the magic of public.
Unity: Starting with 2D Physics
As the name implies, 2D means working in two dimensions. As such, we don't really care about what's going on in the third dimension (or the Z-axis.) As far as we're concerned, we only need to care about the X axis (what's going on horizontally) and the Y axis (what's going on vertically).
In fact, here's a little thing you can try out before we get started. In Unity, try clicking on the 2D button on the top-left of the scene view.
What you'll see is something like this:
You'll notice that Unity is actually just making us think that we're working in 2D when in reality we're actually just making a flat 3D game! We can actually use this trick that Unity uses to our own advantage, as we'll see later on when we discover Lighting effects.
Alright, time to get started. Here's what we'll be working with:
- Rigid-bodies and Forces
- Colliders and Collisions
- Your first game
We will explain what Unity vectors and forces are here, and from the next lesson onwards, we will start off with Rigidbodies and Collisions.
In Unity, we have two scripting classes, named Vector2 and Vector3(Actually, there's a Vector4 as well, but we're not really concerned with that for the time being). Vector2 and Vector3 are what you can call
containers for a number of numeric values, most often floating point values, represented by the number on the end. So, Vector2 is a container for two individual values, and Vector3 is a container for 3 values. When we work in Unity2D, the Vector2 class becomes prominent, although Vector3 still has some uses.
Since this might be a bit confusing for some readers, let's understand with an example. Let's say we have the position of an object, say Mr. Star. His position in the 2D game world is defined by an X value (that is, horizontally) and a Y value (vertically).
In writing code for Unity, instead of making two separate values for the position of Mr. Star, we use a Vector2 which holds two variables instead. These variables can either be defined, or can be set to other user-defined variables.
NOTE: Don't worry about where to write your C# code, we will show you how to add your own scripts soon.
Of course, a Vector2 isn't really limited to defining a position. A Vector has its best use whenever you're dealing with values that modify (or define) the location, rotation or direction of something.
Note: If you didn't install Visual Studio when you installed Unity, your scripts will most likely open in Unity's default interface, MonoDevelop. Don't worry if code looks different than yours in color or something like that; as long as you're following what's going on, you'll be fine.