【问题标题】:How to make Unity3D Camera focusing on one area?如何让 Unity3D Camera 聚焦在一个区域?
【发布时间】:2015-04-10 16:41:38
【问题描述】:

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

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

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

【问题讨论】:

  • 只是为了澄清问题,您想查看不同分辨率的完整版块,对吗?那么,如果分辨率较小,您不希望板被剪裁吗?
  • 这正是我想要的。
  • 我想只有你需要根据当前屏幕比例和默认屏幕比例调整相机FieldOfView
  • @FouadWahabi 你能写一个完整的有条理的答案,这样我可以更好地理解事情吗?
  • 这可能会有所帮助:en.wikipedia.org/wiki/…

标签: c# unity3d


【解决方案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;
            }
        }

    }

}

【讨论】:

    【解决方案2】:

    你能试试 SmoothLookAt 或 transform.LookAt 吗?您可以将两者都放在相机上,并将您的电路板指定为“目标”。你也可以做一个 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 之间,但您可以使其更精细,将其附加到相机,您可以使用滚轮进行控制。我也有用于缩放和移动的触摸脚本。这将允许您放大和缩小。:

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

    【讨论】:

    • C# 不是 javascript(见问题标签)。另外,我不想瞄准任何游戏对象(因为我的棋盘是动态创建的并且由许多其他游戏对象组成),而是我希望我的相机聚焦在定义为遵循 0 &lt; x &lt; 12 和 0 &lt; z &lt; 12 和 y = -1 的平面上.
    • 它是 UnityScript,但 C# 移植应该不会那么难。你可以创建一个中心点实例化游戏对象以便保存它吗?或者你可以同样一个 Vector3();静态访问中心点?
    猜你喜欢
    • 2023-04-09
    • 2011-02-06
    • 2020-08-24
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多