如何使Unity3D相机专注于一个区域



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

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

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

这里有一个例子:

    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;
            }
        }
    }
}

你能尝试一下SmoothLookAt或转换吗?看?您可以将两者放在相机上,并将电路板指定为"目标"。您还可以执行 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.MoveUpdate(gameObject, iTween.Hash("position",board.position,"time",2));
iTween.LookUpdateModified(gameObject,iTween.Hash("looktarget",board.position,"time",2));

您还可以使用其他一些相机脚本。

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

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

相关内容

最新更新