Now that we have gone through the fundamentals of 3D development with Unity3D, we can now cover the basics. The basics will cover first person view, third person view, and collision detection. As usual my tutorials will be broken down into several sections. First section will cover what first person view is and how to achieve it using code. Second section will cover what third person view is and how to achieve it using code. The third and final section is all about collision detection in 3D. As a warning to all of my readers do not to use the code provided in a game of their own. It is buggy and unfinished. The code is for demonstrative purposes only.
Source files for this tutorial is broken into parts using WinRar and are located Here and Here
become a professional vr game developer with unity
Pre-Order The Complete Virtual Reality Game Development with Unity and learn to create immersive games and experiences by building 10 VR games. The course assumes no prior Unity or VR experience – We’ll teach you C#, Unity and 3D programming from the ground-up
Section 1: First Person View
First person view is not as complicated as you might think. Rather, it is actually super simple. First person view means that the player IS the camera, or to simplify, The camera IS the player.. The implications of this thought process simplifies everything as a designer and as a programmer.
So, how do we begin to utilize this knowledge within Unity3D? We could start by building a 3D scene and setting up the scene for testing. I think that would probably be best considering if we do too much for a basic course, things could get very confusing.
Setting up the Scene:
Open a new project, make sure 3D is selected and name it “3D Development Basics”
Our screen should look like so:
Select Window in the menu bar. In the drop down box, select Asset Store.
Inside of the Asset store, sitting next to game view, type in Moon Landing.
Select Moon Landing and click download
After a few moments, it will display a little box. Click on import. This will take a little bit to do.
Now that the Moon LandScape folder has been added to your project, go ahead and add the Scene and Scripts folder. Also create a C# script called First Person Controller.
Inside the Moon Landscape folder, it will have 3 other folders. Demo Scene, Terrain, and Textures respectively.
We will be utilizing this Demo Scene as a means to have a terrain added in it for us. So go ahead and open the Demo Scene folder and load the Demo Scene.
Editing and Adding to the Scene:
The demo scene does not have a Camera attached which suits our purposes just fine. To simplify things, I went ahead and changed the X and Z positions from -500 to 0.
right click on the Hierarchy Pane and select camera
Select the camera in the Hierarchy pane, there are a few things we need to change in the Inspector Pane to have the camera set up correctly. Position X should be 368.6, Position Y should be 52, Field of View should be 26.4862. Everything else is set to default
We have done all we can with setting up and editing the scene so far. We can now move on to the scripting side. I have tried to do an extremely basic approach to scripting to allow the camera to move with the event of using the mouse and keyboard. Since we plan on attaching the script directly to the camera, we can use the this keyword or omit the this keyword and just write the transform.
We want a public float of turnspeed so we can modify it in the editor to tweak as needed. We want a private void method called Mouse Aiming for controlling how to handle mouse movement. We also want a private void method called Keyboard Movement for controlling player movement from keyboard presses. Lastly, we want a private void Update method. This update method will only be used to call the Mouse Aiming and Keyboard Movement methods respectively.[Warning! Do not to use the code provided in a game of their own. It is buggy and unfinished. The code is for demonstrative purposes only.]
public class FirstPersonCamera : MonoBehaviour
public float turnSpeed = 50f;
var rot = new Vector3(0f, 0f, 0f);
// rotates Camera Left
if (Input.GetAxis("Mouse X") < 0)
rot.x -= 1;
// rotates Camera Left
if (Input.GetAxis("Mouse X") > 0)
rot.x += 1;
// rotates Camera Up
if (Input.GetAxis("Mouse Y") < 0)
rot.z -= 1;
// rotates Camera Down
if (Input.GetAxis("Mouse Y") > 0)
rot.z += 1;
transform.Rotate(rot, turnSpeed * Time.deltaTime);
var sensitivity = 0.01f;
var movementAmount = 0.5f;
var movementVector = new Vector3(0f, 0f, 0f);
var hMove = Input.GetAxis("Horizontal");
var vMove = Input.GetAxis("Vertical");
// left arrow
if (hMove < -sensitivity) movementVector.x = -movementAmount;
// right arrow
if (hMove > sensitivity) movementVector.x = movementAmount;
// up arrow
if (vMove < -sensitivity) movementVector.z = -movementAmount;
// down arrow
if (vMove > sensitivity) movementVector.z = movementAmount;
// Using Translate allows you to move while taking the current rotation into consideration
Back to the Editor:
Now, we can attach the script to the camera by dragging and dropping the first person script onto the camera in the Inspector Pane
Save the scene in the Scene folder under the name First Person. To do this, click File, Save Scene As.
Now you can go ahead and run the scene. Voila, you now have a scene where the player is the camera. This is the overall basis for a first person view game.
Section 2: Third Person Perspective
It is extremely easy to over complicate the thought process for a third person perspective game and with good reason. My first assumption was to have the camera follow the player similar to the fashion that would occur in a 2D platformer. But, in reality, that doesn’t make sense when you think about it from a 3D perspective. So, what does make sense and work?
It can be as simplistic or complicated as you want it to be. We should go over a few examples. There are various methods that you can use for third person perspective.
One method is taking the first person view camera and shifting it over the shoulder or behind the player model. This method’s concerns are where the character is facing and collision detection.
Another method is the overhead view. The overhead view does not care whether the player is facing the camera or not.
A third method is a fixed angle camera. It doesn’t care where the player faces, it only cares about whether or not the player is in the room or not.
The last method I want to mention is a CCTV camera. Think about this as a fixed angle camera that acts like a surveillance camera with a TV showing the player what’s going on. This method only cares about whether or not the player is in the room.
There are plenty of other third person view cameras that could be utilized. But the ones listed should give you a base idea of the difference between Third Person and First Person perspectives in addition to triggering some memories of games you have played in the past or currently, and make you think about how they decided to go with that particular game’s perspective.
The third person perspective method we will utilize in this tutorial is over the shoulder / behind the player.1
Building the Scene:
Again we will be utilizing the Demo Scene as a basis in order to have a terrain added in it for us. So go ahead and open the Demo Scene folder and load the Demo Scene.
We will add a camera to the scene.
Click on the Camera and look at the Inspector Pane for it. The Initial values need to be changed.
Let’s change the X position to be -489.9, Y to be 38.4, and Z to be -105.6 on the Camera control within the Inspector. We also want to change the camera’s Y rotation to be 90.
Now click on Create, 3D Object, Cube. And add it to the Hierarchy Pane. This will be our “character” for this 3rd person view. We also need to change the Position and Scale of the Cube to be set up properly with the camera. X position should be -482.5, Y Position should be 36, and Z position should be -106.6. The X, Y, and Z scale should be set to 2.
Drag the Cube onto the Camera, this should make the Cube become a child of the Camera component.
Scripting: Creating the third person controller
[Warning! Do not to use the code provided in a game of their own. It is buggy and unfinished. The code is for demonstrative purposes only.]
(Third Person Scriptusing UnityEngine;
public class ThirdPersonCamera : MonoBehaviour
public GameObject character;
transform.position += new Vector3(0, 0, 1);
transform.position -= new Vector3(0, 0, 1);
transform.position += new Vector3(1, 0, 0);
transform.position -= new Vector3(1, 0, 0);
Drag the third Person Camera Script onto the Camera. Then the character property should have the cube component dragged onto it.
Section 3: Collisions
Collision detection has been made a lot easier with Unity3D and other game development engines. What makes collision detection easier within Unity3D is the options available to you. You have Box colliders, mesh colliders, Rigidbody, sphere collider, capsule collider, wheel collider, terrain collider, cloth, hinge joint, fixed joint, spring joint, character joint, configurable joint, and constant force. These are the ones for 3D alone.
If you want to make a 2.5D or 3D game that also has 2D elements, there are more options available. Rigidbody 2D, Circle Collider 2D, Box Collider 2D, Edge Collider 2D, Polygon Collider 2D, Spring Joint 2D, Distance Joint 2D, Hinge Joint 2D, Slider Joint 2D, Wheel Joint 2D, Constant Force 2D, Area Effector 2D, Point Effector 2D, Platform Effector 2D, and Surface Effector 2D.
Each one of these options has its own particular use and whilst we won’t go over each and every one, I will be sure to cover the ones that you will mainly use in basic game development.
The main ones that you will use in a basic 3D game is the Box colliders, Mesh Colliders, Rigidbody, Sphere Collider, and Terrain. I feel like we should talk about these.
A Box Collider is a basic cube-shaped collision primitive. In other words, it is shaped just like a cube would be. So basic uses for it would be a chest, floor, walls, or if you wanted to use as much processing power you could, each individual part of a 3D model.
A Mesh Collider builds a collider based on the Mesh Asset from a 3D model. It is one of the most accurate collision detection methods and has the ability to collide with other mesh colliders if you choose.
One of the most used and recognizable collider types. Rigidbody colliders allow you to enable GameObjects to act under the laws of physics within a game environment. I should also note that it can interact with objects through the NVIDIA PhysX engine as well.
Sphere Colliders are a basic sphere shaped collision primitive, in other words, shaped like a sphere. These can be used on balls, fist, heads, and et cetera.
The Terrain Collider creates a collision surface that holds the same shape as the Terrain object it is attached to.
As I have shown above, there are plenty more physics objects that are used from within Unity3D. Later tutorials will cover these as needed.
Each of the different physics options have a script attached to them that you cannot access or modify. However, when needed, you can create a controller script that can handle collisions on your own terms if you so choose.
Now that we have gone over some of the basic physics objects to handle collision, it is time for us to actually implement this in a scene.
This section of the tutorial is extremely short and very cool to say the least. Because we are using built in objects within Unity3D, we don’t have to do any scripting whatsoever.
To start off, we make a brand new scene.
Click on the camera and take not the the X,Y, and Z positions of it. These figures are immensely important.
Right click on the Hierarchy Pane, highlight 3D object, and select Plane.
Right click on the Hierarchy Pane again, highlight 3D object, and select Cube.
Select the Main camera and look at the Camera Preview in the bottom right of the Scene view. Neither the plane or the cube shows up. We need to fix that.
To make this part easier, I went into the Game view. Select Plane, we need to modify the Y Position of its transform to 0.8.
Notice the Plane has a Plane( Mesh Filter), Mesh Collider, and Mesh Renderer. The Mesh Collider is important with physics implementation.
Select the Cube and in the Inspector Pane, we need to Change the Y and Z position of its transform. Y is set to 2, and Z is set to -8.
Notice the Cube has a Cube (Mesh Filter), Box Collider, and Mesh Renderer as well.
If you were to select the play button right now, you will notice that the box hangs in midair and defies gravity. There is one last step we need to complete before gravitational physics can take place. We need to add a rigidbody to the Cube.
To do this, click on Add Component in the Inspector Pane of the Cube and select Physics.
Now, select RigidBody component.
Notice the RigidBody has a few properties that have populated within the Cube in the Inspector Pane. Mass, Drop, Angular Drag, Use Gravity, Is Kinematic, Interpolate, Collision Detection, and Constraints.
Mass should be set to 1, drag 0, Angular Drag 0.05, Use Gravity should be checked, Is Kinematic is unchecked, Interpolate should be none, Collision Detection is Discrete, and Constraints should have nothing checked.
Congratulations, you have now added some rudimentary physics to your game!
As you can see, the basics of 3D game development in itself can be a complex and daunting task. In the near future, we will go into much more depth and create a 3D game which houses all of the elements used within each tutorial that I upload. I hope you enjoyed this tutorial and look forward to the next one, until next time… “May your code be robust and bug free.”