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.
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.
To create an inspect camera, follow these steps:
inspectScene
Unity package and open the scene available in the 02_06
folder, also named inspectScene
.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.MouseOrbit
script and rename the new copy to InspectCamera
.InspectCamera
script in your editor.@script
, insert the following code:var zoomInLimit = 2.0; var zoomOutLimit = 1.0; private var initialFOV : float;
@script
to:@script AddComponentMenu("Camera-Control/Inspect Camera")
Start
function begins:initialFOV = camera.fieldOfView; transform.position = new Vector3(0.0f, 0.0f, -distance) + target.position;
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; } }
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.
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.
18.222.93.132