Making an inspect camera

Inspect cameras are a very popular way of displaying products online. These virtual displays usually feature a camera that orbits around and zooms in and out the product. In this recipe, we will learn how to implement such a camera using a standard Unity component as a starting point.

Getting ready

In order to follow this recipe, please import the inspectScene package, available in the 0423_02_06 folder, into your project. The package includes a basic scene containing the 3D model of a mobile phone and a camera.

Note

Please note this recipe involves editing a JavaScript file provided by Unity. In case you want the C# version of the final script, we have included it in the 0423_02_06 folder.

How to do it...

To create an inspect camera, follow these steps:

  1. Import the inspectScene Unity package and open the scene available in the 02_06 folder, also named inspectScene.
  2. Add a directional light to the scene, making it cast light in the same direction the camera is facing.
  3. In the Hierarchy view, drag the Directional Light into the Main Camera game object, making it a child of the Main Camera game object.
  4. Make sure the MouseOrbit script is available at the Project view (it should be inside the Standard Assets | Scripts | Camera Scripts folder). If not, import the Scripts package by navigating to Assets | Import Package | Scripts.
  5. Duplicate the MouseOrbit script and rename the new copy to InspectCamera.
  6. Open the InspectCamera script in your editor.
  7. Insert, before the line starting with @script, insert the following code:
    var zoomInLimit = 2.0; 
    var zoomOutLimit = 1.0; 
    private var initialFOV : float;
  8. Then, change the line starting with @script to:
    @script AddComponentMenu("Camera-Control/Inspect Camera")
  9. Also, add the following lines of code right after the Start function begins:
    initialFOV = camera.fieldOfView; 
    transform.position = new Vector3(0.0f, 0.0f, -distance) + target.position;
  10. Then, replace all of the LateUpdate function with the following code:
    function LateUpdate () {
       if (target && Input.GetMouseButton(0)) {
    		if(Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)){
     			var zoom = camera.fieldOfView - Input.GetAxis ("Mouse Y");
    			if(zoom >= initialFOV / zoomInLimit && zoom <= initialFOV / zoomOutLimit){
      				camera.fieldOfView -= Input.GetAxis ("Mouse Y");
    			} 					  
     		} else {
    			x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
                 } 
         y = ClampAngle(y, yMinLimit, yMaxLimit);
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
       	transform.rotation = rotation;
        	transform.position = position;
        }
    }
  11. Save the script. Then, select the Main Camera option in the Hierarchy view and add the Inspect Camera component by navigating to Component | Camera-Control | Inspect Camera, as shown in the following screenshot:
    How to do it...
  12. The Inspect Camera component should now be available in the Inspector window for the Main Camera. From the Hierarchy view, drag the mobile game object into the Target slot. Then, change the Distance value to 1:
    How to do it...
  13. Play the scene. Move the mouse and keep the left button pressed to look around the object. To zoom in and out, keep the left button and the Shift key pressed, and move the mouse vertically.

How it works...

Unlike Unity's MouseOrbit, our InspectCamera script checks if the mouse button is pressed as a condition to move the camera. We have also implemented a zooming feature that changes the value of the camera's Field of View when the Shift key and a mouse button are pressed, and the mouse is moved vertically.

There's more...

If you want a C# version of the script, don't worry; we have included it in the 0423_02_07 folder. Also, should you need to use another key instead of Shift, check out Unity's documentation for the appropriate key codes at http://docs.unity3d.com/Documentation/ScriptReference/KeyCode.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216