2.6 制作一个检视摄像机 Unity
Unity技术Thepoly 14502 0
实名

通过了实名认证的内容创造者

发布于 2022-3-24 18:24:01

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 Thepoly 于 2022-3-24 20:07 编辑

src=http___xqimg.imedao.com_17b70f518351426d3fd73b36.png!800.jpgrefer=http___xqimg.imedao.webp.jpg
174551k9r66ex7z719e99h.jpg
Hello,大家好。
周一到了Kinglary分享
游戏引擎相关内容
2.6 制作一个检视摄像机
2.6 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.
检视摄像机经常用于在线展示产品。这些可视化展示赋予摄像机依据轨道环绕、放大和缩小产品的功能。在本章节内容中,我们将学习如何用标准的unity组件,创建一个检视摄像机。
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.
为了完成本章节内容,请在0423_02_06文件夹中获取 inspectScene包并导入到你的工程项目之中。这个包,包含一个手机的3D模型和一个摄像机
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.
注意:请注意,本节内容是用JavaScript编写,如果你想使用C#脚本编写版本,在文件夹 0423_02_06中已经有了该脚本文件。
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.
1. 在02_06文件夹中打开场景,导入inspectScene包,也将其命名为inspectScene。

2. Add a directional light to the scene, making it cast light in the same direction the camera is facing.
2. 在场景中添加一个平行光源,并让它的投光方向与摄像机朝向一致。

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.
3. 在Hierarchy面板中,将Directional Light 拖拽到Main Camer游戏对象下,使其成为Main Camer的一个子对象。

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.
4. 在Project面板中,确认MouseOrbit脚本可以使用。(该脚本应该在Standard Assets | Scripts | Camera Scripts文件夹下)。如果不能,点击Assets | Import Package | Scripts导入Scripts包。

5. Duplicate the  MouseOrbit script and rename the new copy to  InspectCamera.
5. 复制MouseOrbit脚本,并将新的复制对象重命名为InspectCamera。

6. Open the  InspectCamera script in your editor.
6. 在编译器中打开InspectCamera脚本。

7. Insert, before the line starting with  @script , insert the following code:
7. 在用  @script开始编写代码前,插入以下代码:
var zoomInLimit = 2.0;
var zoomOutLimit = 1.0;
private var initialFOV : float;

8. Then, change the line starting with  @script to:
8. 然后,用@script改变起始行为:
@script AddComponentMenu("Camera-Control/Inspect Camera")

9. Also, add the following lines of code right after the  Start function begins:
9. 在Start函数开始后,添加如下代码:
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:
10. 然后,用以下代码替换掉LateUpdate函数中的所有内容:
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:
11. 保存脚本,然后在Hierarchy面板中选择Main Camera选项,通过点击Component | Camera-Control |Inspect Camera添加Inspect Camera组件,如截图所示:
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:
12. 在Inspector窗口中,我们可以看到Main Camera下,Inspect Camera组件是可视的。我们将Hierarch的mobile游戏对象拖拽到Target 项目上,然后将Distance的参数改为1:
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.
13. 运行游戏场景,在按住左键的状态下移动鼠标环看游戏对象。在左键和shift同时按住的状态下垂直移动鼠标,进行放大和缩小。
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.
不像unity的MouseOrbit脚本,我们的InspectCamera满足以点击鼠标为条件来实现摄像机移动。并且,在鼠标和shift同时按下时,实现了缩放摄像机视野的功能
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.
如果你想用C#进行编写,不要担心,在0423_02_07文件夹下已经备份了。


本帖被以下画板推荐:

还没有设置签名!您可以在此展示你的链接,或者个人主页!
使用道具 <
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表