如何让Unity3D Camera聚焦在一个区域?

How to make Unity3D Camera focusing on one area?

在我的 Unity3D 项目中,我有一个动态创建的板(使用 c#)并位于屏幕上,如下所示:

0 < x < 12
0 < z < 12
y = -1

如何让摄像头聚焦在电路板上,并针对不同的屏幕分辨率和不同的平台使其居中?

你能试试 SmoothLookAt 或 transform.LookAt 吗?你可以把两者都放在你的相机上,并将你的板指定为你的"target"。您还可以执行 iTween,让您看起来正在平滑地查看指定目标。

private var target : Transform;
var damping = 6.0;
var smooth = true;

@script AddComponentMenu("Camera-Control/Smooth Look At")

function LateUpdate () {
    if (target) {
        if (smooth)
        {
            // Look at and dampen the rotation
            var rotation = Quaternion.LookRotation(target.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
        }
        else
        {
            // Just lookat
            transform.LookAt(target);
        }
    }
}

function Start () {
    // Make the rigid body not change rotation
    if (target == null)
         target = GameObject.Find("Board_Name").transform;

    if (GetComponent.<Rigidbody>())
        GetComponent.<Rigidbody>().freezeRotation = true;
}

通过 iTween,您可以使用

iTween.MoveUpdate(gameObject, iTween.Hash("position",board.position,"time",2));
iTween.LookUpdateModified(gameObject,iTween.Hash("looktarget",board.position,"time",2));

还有一些您可能会使用的其他相机脚本。

前阵子我也做过一个鼠标平移缩放类型的脚本。请记住 minZoom 和 maxZoom 必须在 0 到 180 之间,但您可以使其更精细,将其附加到相机,您可以使用滚轮进行控制。我还有用于 Zoom 和移动设备的触摸脚本。这将允许您放大和缩小。:

if (Input.GetAxis("Mouse ScrollWheel")!=0) {
    camera.fieldOfView += Input.GetAxis("Mouse ScrollWheel");
    camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, minZoom, maxZoom);
} 

这是一个例子:

    using UnityEngine;
using System.Collections;

/**
 * This class attempts to force VERT- Field of view scaling.
 * By default, Unity uses the HOR+ technique.
 * 
 * http://en.wikipedia.org/wiki/Field_of_view_in_video_games#Scaling_methods
 */

[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class VertMinusCameraFOV : MonoBehaviour {

    public float designTimeVerticalFieldOfView = 60;
    public int designTimeWidth = 1280; // default screen width
    public int designTimeHeight = 720; // default screen height

    private float hFOVInRads;

    private int prevWidth;
    private int prevHeight;

    void Start () {

        prevWidth = designTimeWidth;
        prevHeight = designTimeHeight;

        float aspectRatio = (float) designTimeWidth / (float) designTimeHeight;
        float vFOVInRads = designTimeVerticalFieldOfView * Mathf.Deg2Rad;
        hFOVInRads = 2f * Mathf.Atan( Mathf.Tan(vFOVInRads/2f) * aspectRatio);

    }

    void Update () {

        if (Screen.width != prevWidth || Screen.height != prevHeight) { // capture screen ratio changes

            float aspectRatio = (float) Screen.width / (float) Screen.height;

            float vFOVInRads = 2f * Mathf.Atan( Mathf.Tan(hFOVInRads/2f) / aspectRatio );

            Debug.Log("Screen resolution change. Recomputing aspect ratio (" + aspectRatio + ") and field of view (" + vFOVInRads*Mathf.Rad2Deg + ")");

            foreach (Camera cam in GameObject.FindObjectsOfType(typeof(Camera))) {
                cam.fieldOfView = vFOVInRads * Mathf.Rad2Deg;
            }
        }

    }

}