[英]How to access the main Camera in Unity to make a zoom script?
I am trying to make a simple zoom script for when you click on a cube. 当您单击多维数据集时,我试图制作一个简单的缩放脚本。 I want it to zoom in on the cube, but I cannot find a way to make the main camera zoom for me.
我希望它在立方体上放大,但是找不到适合我的主相机缩放方法。 I have tried several different ways.
我尝试了几种不同的方法。 Here is the current one.
这是当前的。 I had it in a
OnMouseDown
, but it still would not work, so I moved it to update to see if I could get it to work. 我将其放在
OnMouseDown
,但仍然无法正常工作,因此我将其移动以进行更新以查看是否可以正常工作。
void Update ()
{
if(Input.GetKeyDown("z"))
{
Debug.Log("Pressed Z");
zoomedIn = !zoomedIn;
}
if(zoomedIn == true)
{
Debug.Log("True!");
Camera.main.GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, zoom, Time.deltaTime*smooth);
}
else
{
Camera.main.GetComponent<Camera>().fieldOfView = Mathf.Lerp(GetComponent<Camera>().fieldOfView, normal, Time.deltaTime*smooth);
}
}
Looks like zoom
and normal
are not assigned to the correct values. 似乎没有将
zoom
和normal
分配给正确的值。 Also make sure that you're in Perspective
, not Orthographic
view. 还要确保您处于
Perspective
视图,而不是“ Orthographic
视图。
If you want to use
Orthographic
view just change all usages offieldOfView
toorthographicSize
and changezoom
to something reasonable, like 5 units.如果要使用
Orthographic
视图,只需将fieldOfView
所有用法更改为orthographicSize
然后将zoom
fieldOfView
更改为合理的水平,例如5个单位。
normal
should be the initial fieldOfView
of the camera, retrieved in Start
: normal
应该是摄像机的初始fieldOfView
,在Start
检索到:
// camera is a private field
private Camera camera;
camera = GetComponent<Camera>();
normal = camera.fieldOfView;
zoom
should be a value less than normal
(initial fieldOfView
) assigned from the inspector to be able to "zoom in". zoom
的值应小于检查员分配的normal
值(初始fieldOfView
),以便能够“放大”。
Your conditional branch will change to 您的条件分支将更改为
if (zoomedIn) // Same as if (zoomedIn == true)
{
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom, Time.deltaTime * smooth);
}
else
{
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normal, Time.deltaTime * smooth);
}
Or, a more concise version: 或者,一个更简洁的版本:
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoomedIn ? zoom : normal, Time.deltaTime * smooth);
I also suggest using a Coroutine to do this instead of doing this in Update
. 我还建议使用协程来执行此操作,而不要在
Update
中执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.