A.B.C.

You are here: Home > Unity 3D > Create a 3D model viewer with Unity 3D

Create a 3D model viewer with Unity 3D

1. Objective

Create an easy 3D model viewer using Unity 3D. This is a tutorial for Unity beginners.

The online demo can be found here.

2. Needed Tools

The following tools are needed:

  • Unity: create the viewer.
  • Brackets: code the C# part.
  • An architecture software: create a 3D model.

2.1 Unity

Unity is cross-platform game engine developed by Unity Technologies.

There’s a free personal edition. Download it from here: https://store.unity.com/products/unity-personal . Once downloaded, install it and create your login.

2.2 Brackets

Brackets is a lightweight, yet powerful, modern, open source text editor. It can be found here: http://brackets.io/

It is used to write the C# code instead of using Microsoft’s Visual Studio that comes with Unity.

3. How-To

3.1 Create a 3D model

Use your preferred architecture software to create your 3D model. Export your model into an FBX file (this should also create textures files).

For this simple tutorial, I downloaded a free FBX file here: https://www.turbosquid.com/Search/3D-Models/free?include_artist=TomaszCGB

3.2 Code the viewer

Launch Unity and create a new 3D project.

Wait for the project to set up.

In the file explorer (on your OS desktop), in the Assets folder, create a new FBX folder and add the FBX file with the textures.

Switch back to Unity. Unity configure now the FBX files.

Drag and drop the 3D model from the Project tab to the Hierarchy tab.

Move and rotate the Main Camera to the start location.

Place the Directional Light.

Create a new C# script. Name it FlyThroughCam.

Open the script. Add two new public objects and remove the Start section (we don’t need it).

public float rotationSpeed = 1;
public float translationSpeed = 1;

Save the file and go back to Unity.

Drag and drop the script on the Main Camera. This adds a script box to the Inspector.

In the script, add the following code into the Update section.

//WASD translation
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward /50 * translationSpeed, Space.Self);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left /50 * translationSpeed, Space.Self);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back /50 * translationSpeed, Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right /50 * translationSpeed, Space.Self);
}

//arrow key rotation
if (Input.GetKey(KeyCode.RightArrow))
{
transform.RotateAround(Vector3.up, rotationSpeed / 50);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.RotateAround(Vector3.up, - rotationSpeed / 50);
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Rotate(Vector3.right, - rotationSpeed);
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Rotate(Vector3.right, rotationSpeed);
}

Now you should be able to move around your 3D model:

  • In Unity, hit the Play button.
  • WASD and the arrow keys to move around.

Publish to the web. And that’s it!

nach oben