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

时间:2015-04-10 16:41:39

标签: c# unity3d

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

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

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

2 个答案:

答案 0 :(得分: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;
            }
        }

    }

}

答案 1 :(得分:0)

你可以尝试SmoothLookAt或transform.LookAt吗?您可以将两者都放在相机上,并将您的主板指定为&#34;目标&#34;。您还可以执行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));

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

我还做了一个鼠标Pan Zoom类型的脚本。请记住minZoom和maxZoom必须在0到180之间,但你可以使它更精细,将它附加到相机,你可以用滚轮控制。我还有Zoom和mobile的触摸脚本。这将允许您放大和缩小。:

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