Introduction to Unity3D - Nuts and Bolts
Developing games in Unity 3D can be fun, especially if you are like me and have struggled with various native code libraries in the past.
Glad you decided to take the plunge, this is a good place to be, you will have fun !!!
Here are some basics to get you started.
Let's start by looking at some variable types in Unity 3D.
1) Vectors (Vector3, Vector2, etc.)
A Vector is the most fundamental way to represent a 3D object along the {x,y,z} axis. It can be used to represent different things though: scale, rotation, position.
e.g. - a Position Vector 3 represents an object's position along the {x, y, z} axis.
- a Scale Vector3 represents an object's size along the {x,y,z} axis
- a texture Scale Vector2 represents the texture's size along the {x,y} axis. Although, if the texture is laying flat on the ground it would represent the texture's scale along the {x,z} axis
Every object in a scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object.
If it is a 3D object, the transform attributes are represented as a Vector3 variable and can be accessed as below:
- To get an object's position along the x axis, use transform.position.x
- To get an object's rotation along the y axis, use transform.rotation.y
A GameObject is the most basic element. You can think of GameObjects as an entity that performs a certain action on the screen.
e.g. Most games have one player, this is one gameobject
Most games have several enemies, these are represented as multiple gameobject instances
A GameObject can have several components:
> A Transform { Position, Rotation, Scale }
> Scripts (code used to act on the GameObject)
Unity provides several in-built physics components so that we can make our players/enemies/props behave realistically with just a few settings.
These in-built objects allow gameobjects to be affected by collisions, gravity and other forces. (i.e. they will be moved by collisions and falls but will not start moving by themselves)
By controlling the physics from scripts, you can give an object the dynamics of a vehicle, a machine or even a moving piece of cloth.
Here are some of those physics components: Colliders, CharacterControllers, RigidBodies. Let's learn more about these now.
Think of a collider as a simple invisible boundary around your gameobject.
In the example below, the image below shows a (green) spherical collider region around the gameobject. We've set the radius to 8.
Once a collider region has been defined around your gameobject, Unity will automatically trigger methods whenever other objects enter the collider region. Here are some of those methods:
> OnCollisionEnter - is called when a collider/rigidbody has begun entering the invisible collider area of our rigidbody/collider.
> OnCollisionStay - is called once per frame for every collider/rigidbody that is within the invisible collider area of our rigidbody/collider.
> OnTriggerExit - is called when the other Collider has exited the invisible collider region of our rigidbody/collider.
e.g. Let's say you have an enemy and you define a large spherical collider around it, then if a player was to step into that spherical collider area the enemy will be notified about this event (via OnCollisionEnter) and can act accordingly.
A collider, which is invisible, need not be the exact same shape as the object's mesh and in fact, a rough approximation is often more efficient and indistinguishable in gameplay. The simplest (and least processor-intensive) colliders and the so-calledprimitive collider types. In 3D, these are the Box Collider, Sphere Collider and Capsule Collider. In 2D, you can use the Box Collider 2D and Circle Collider 2D. Any number of these can be added to a single object to create compound colliders. With careful positioning and sizing, compound colliders can often approximate the shape of an object quite well while keeping a low processor overhead.
Further flexibility can be gained by having additional colliders on child objects (e.g. boxes can be rotated relative to the local axes of the parent object). However, you should be sure that there is only one Rigidbody and this should be placed on the root object in the hierarchy.
There are some cases, however, where even compound colliders are not accurate enough. In 3D, you can use Mesh Colliders to match the shape of the object's mesh exactly.
A Rigidbody is the main component that enables physical behaviour for an object. With a Rigidbody attached, the object will immediately respond to gravity. If one or more Collider components are also added then the object will be moved by incoming collisions.
A gameobject should only have one rigidbody component.
A RigidBody has several variables that can be set to establish some ground rules for the gameobject.
> If you want the gameobject to respond to gravity, click on the "Use Gravity checkbox"
> If you want the gameobject to be limited to only certain axis of movement, you can restrict it by click on the "Freeze Position" checkboxes as shown above.
> You can make the object heavier by setting a Mass value.
Since a Rigidbody component takes over the movement of the object it is attached to, you shouldn't try to move it from a script by changing the Transform properties such as position and rotation. Instead, you should apply forces to push the object and let the physics engine calculate the results (e.g. say you want a ball to respond to external events such as wind or force).
There are some cases where you might want an object to have a Rigidbody without having its motion controlled by the physics engine. For example, you may want to control your character directly from script code (like in the case of an enemy movement) but still allow it to be detected by triggers (see Triggers below).
This kind of non-physical motion produced from a script is known as kinematic motion.
The Rigidbody component has a property called Is Kinematic which will remove it from the control of the physics engine and allow it to be moved kinematically from a script.
It is possible to change the value of Is Kinematic from a script to allow physics to be switched on and off for an object, but this comes with a performance overhead and should be used sparingly.
6) Character Controllers (Physics)
The character in a first- or third-person game will often need some collision-based physics so that he doesn't fall through the floor or walk through walls. Usually, though, the character's acceleration and movement will not be physically realistic, so he may be able to accelerate, brake and change direction almost instantly without being affected by momentum.
In 3D physics, this type of behaviour can be created using a Character Controller. This component gives the character a simple, capsule-shaped collider that is always upright. The controller has its own special functions to set the object's speed and direction but unlike true colliders, a rigidbody is not needed and the momentum effects are not realistic.
A character controller cannot walk through static colliders in a scene, and so will follow floors and be obstructed by walls. It can push rigidbody objects aside while moving but will not be accelerated by incoming collisions. This means that you can use the standard 3D colliders to create a scene around which the controller will walk but you are not limited by realistic physical behavior on the character itself.
If you are still with me and you want to engage your left side and right side of the brain then march forward. The best way to do that is to build a game from the ground up.
Here's a 5 part tutorial to get you started with the basics of game development:
Creating Smart Enemy Characters Using Unity