Physics In Unity

Introdouction

Unity is a cross-platform game engine used for game development. With game development comes all of the possibilities videogames provide. Any interaction within the game any inputs used by the user can be used to create different types of experiences. However the importance of these interactions come in the form of game physics. Game physics decides the rules of the game and is responsible for creating all of the interactions that we are familiar with. This blog will dive into the Unity Physics engine to explain the fundamentals of physics in Unity and create projects to show examples of how physics affect video games.

Unity Physics

Interactions like gravity, friction, collision are some of the elements that the Unity Physics Engine provides. When it comes to  these physics elements two topics combine to make these possible Rigidbody and Colliders, these two elements combine to try and recreate physics in the real world within the game.

Rigidbody 

Rigidbody is what allows a game object to interact with or react to other game objects with realistic physics effects. It also incorporates gravity to the game object which creates the notion of falling.

Figure 1: Rigidbody Properties
Rigidbody also allows the developer to change many aspects of the objects to create different interaction with one another. As seen in Figure 1, the properties of mass, drag, angular drag, Use gravity, is Kinematic, Interpolate, Collision Detection and Constraints can be changed. When two game objects collide, Rigidbody is what creates an interaction between the two. 

Important Properties of Rigidbody


Mass

Mass is what controls the game objects weight, The Unity Physics Engine measures mass by kilograms and will simulate a realistic situation whenever objects interact with one another with different masses. The higher the difference in mass, the stronger the reaction of the object with less mass. Shown in the video below.

Drag

Drag is what applies friction to the game objects, this determines the amount of resistance that the game objects apply as they are moving in any direction. Higher drag will cause an object to move slower and stop faster. An example of this is when falling the drag will have an effect like friction with the air and slow the decent of the game object creating a game object that falls slower than usual. Shown in the video below.



Use Gravity

This feature enable or disables gravity of a game object. This means any force that is applied to the game object when gravity is off is permanent (unless acted upon by another force), and will move with the game object with the same force that was applied in the same direction forever. Shown in the video below.



Is Kinematic

This feature if enabled will disable the Unity Physics engine from acting upon the game object. This works like colliders within Rigidbody but is still able to be moved by the using scripts. Shown in the video below.




Colliders

Figure 2: Types of Colliders

Colliders also causes interactions between game objects. The difference between Rigidbody is that this element is static and also determines the shape of the game object. Without colliders any game object with rigid body will just fall off the world or would be off balance whenever there is an interaction.

The collider components shown on figure 2 shows the many shapes the colliders come in, either in 2D or 3D. 

In the example covered below, the video shows how Rigidbody reacts to game objects with no Colliders, just going through the floor and not interacting with it.







Player Movement

Figure 3: Game Collision
With Rigidbody and Colliders the player will be able to interact with the world depending on the movement controls of the game. In Figure 3, the player is able to use the vehicle in game to crash through the obstacles.

Movement is created using scripts, In Unity in order to create scripts the developer must use the C# programming language. Many of the objects created within the game apply to the class MonoBehaviour, This class is what allows easy access to new programmers within Unity because in the MonoBehaviour class many of the complex code that need to be written is already made within this class in Unity.

Code in Unity

As mentioned above the primary way to program in Unity is by using the programming language C#. Since I have some experience in Java and the fact that C# and Java have a lot of similarities this language was easy to understand and create code.

Figure 4: Player Movement
Using the Code in Figure 4, I am able to program a game object to move forward and  turn within the game with simple inputs. The transform class is what enables the movement of the game object because this is what tells the game where to move. Easily being able to change the speed and turn speed allows for more inputs to be tested.
 This code combined with Rigidbody and colliders enables the game object to interact with obstacles, crashing, falling and going around the in game world with the laws physics slightly applied.

Figure 5: Following Camera
Another code I used in Figure 5 is the ability to follow the game object whenever the player uses an input to move it. Using Vector3 and creating a position for the game camera using this and the transform class the camera now follows the game object with a prober perspective to be able to go around the game world and simulate physics.



Conclusion

There are many possibilities with the Unity Physics engine, and I am just getting started. The interactions and different types of Physics engine within Unity will be covered within the next blog with even more collisions and interactions within game objects.