Unity Physics in Action

Unity Learning

Unity has a great introductory course within their official unity learn website, also providing assets for the users to be able to create code without needing to be creative with the objects that they are coding. The course that I have decided to do is a simple project for beginners that allows the user to program a vehicle object that is able to move around certain obstacles and interact with them. 

Utilizing Physics in Game

Interaction with other objects are simulated by the Unity Physics Engine. Although not being realistic at times, the results are always interesting and makes you appreciate any game you play and how the people behind these games are able to simulate physics in their games. The basic functions of the game will be player movement, camera hover and obstacle interaction. Utilizing the Physics in game I am able to change certain features of the obstacles to react differently with the vehicle object.

Rigidbody Manipulation

Figure 1: Rigidbody Properties
Rigidbody is a component that allows a game object to interact with or react to other game objects. Manipulating Rigidbody is easily done by changing the property values of the in game objects. The most noticeable changes within Rigidbody is the changes to the properties of Mass and Gravity. These properties allow collisions with the vehicle object and obstacle objects to escalate even more and create great Physics simulation examples. 

Unity Prototype and Assets

Figure 2: Vehicle
Assets
Downloading the first prototype with Unity Learning and importing Assets and packages does not take up much time as you start with the project. Starting up Unity, a road is already placed for the user along with a few vehicles and obstacles to choose from the assets. For my project I decided to use the bus for the vehicle and the boxes for the obstacles. The shape of these assets are important since this is a 3D project. I plan on balancing the obstacles on top of one another to create different reactions and interactions with one another.



Positioning Assets

Figure 3: Obstacle Assets
Positioning the vehicle and obstacle objects can easily be done by drag and drop method to the game scene. The positioning of any of the assets does not matter for me except for the vehicle object. The vehicle object will have to be on the start of the road so it will be easier for me to program later. As for the obstacle objects these are made to be obstacles, so I just used them to get in the way of the vehicle object. With the assets in place I am able to start programming to create Physics interactions within unity.

Figure 4: Camera Icon
Camera Control

Before any programming can be done, I first used the camera controls to be able to view the actual game in progress while it is running. The camera will also be programmed later on to be able to follow the vehicle while the game is running.

Programming in C#

Figure 5: Player Movement
The primary way of programming in Unity is the programming language C#. Using the code in Figure 5 I am able to move the vehicle object forward and turn sideways. The transform class from unity is what enables movement of the vehicle object. The transform class is what tells the game where and how to move. Turn speed as well the base speed of the vehicle object can always be adjusted. With different vehicle assets I am able to try different combinations to test what feels the best when creating the game. The perspective can also change when I program the Camera to follow the player. As seen in Figure 6, Using an offset position I am able to stay on top of the vehicle that the player is controlling while there are inputting different commands.

Figure 6: Following Camera

Physics Simulations

All of the pictures and words above were all leading up to this, the physics simulations. My favorite part of the whole project is testing out what would happen if I change different aspects of the different properties of Rigidbody. Now I will be showing some of the physics or lack of, in this unity project. This will include somewhat accurate forces acting upon one another while also showing how the code reads and interprets physics within the unity game engine and how it follows its own rules.

Unity Views

Before I show some of the simulations I am first going to talk about the two types of views that I am using in order to clarify what is happening. The first view is the game view, this view is the actual game and cannot be edited. The second view is the scene view, this view is what allows me to adjust the game and is where most of the development takes place. The scene view also runs with the game and will be used as a second camera in the simulations. I will be showing two perspectives one behind the vehicle object and one behind the obstacle objects to show more of the simulation.

Unity Physics Simulation 1: Low Mass

In this simulation the obstacles in front of the vehicle have little to no mass with gravity enabled. As you can expect the obstacles will go flying, but it gets better without gravity. 

Unity Physics Simulation 2: No Gravity

Without gravity to bring the obstacles down, they will continue to flying until the game resets.

Unity Physics Simulation 3: Heavy Mass



In this simulation I decided to turn off gravity while the obstacle objects have extremely heavy mass compared to the vehicle object to show a fun interaction where the vehicle object passes through the obstacle objects. This is because Newton's third law which is "for every action there is an equal and opposite reaction" is not applied within the game. The vehicle object only follow what is written in the code which is advancing every time that the user inputs its forward movement.

Conclusion

Overall I had a lot of fun creating and programming this project. The knowledge that I learn within this project will help me with future endeavors by teaching me a good amount of C# within Unity. Also having more appreciation for any game developer being able to program physics within a game is very challenging even though some forces are already premade. My journey with Unity is just beginning and as I continue, I will always look back on this project, simulating the Unity Physics Engine.

Comments