使物体的视觉尺寸保持恒定,而不管距离如何 [英] Make object visual size constant despite the distance

查看:166
本文介绍了使物体的视觉尺寸保持恒定,而不管距离如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处提供了视觉帮助

请参阅视觉帮助.在gif之后,有两个GUI.左侧的GUI正在根据摄像机到对象的距离正确缩放.右侧的GUI正在缩放,在场景"视图中清晰可见,但在游戏视图中甚至不明显.该应用程序适用于Hololens,因为用户可以在Unity场景中控制相机.

Please see visual aid. Following the gif, there are two GUIs. The GUI on the left is scaling properly based on the cameras distance to the object. The GUI on the right is scaling, seen clearly in the 'Scene' view, but not even noticeable in game view. The application is for the Hololens as the users head controls the camera within the Unity scene.

问题在于设置对象缩放比例.一整天都在绞尽脑汁,试图复制左侧GUI上显示的缩放比例类型.划分距离并将其用作缩放比例的因素显然是不正确的,因为对象要么太大,增长得太快,要么变得太小而增长得太慢,但我不知道该如何在中间某个地方相遇.在两个方向上均等地扩展,但幅度并不明显,但也不令人讨厌.

The problem lies in setting the objects scaling. Been racking my brain all day trying to replicate the type of scaling shown on the left GUI. Dividing the distance and using this as the factor to scale by is clearly incorrect as the object is either far too large and grows too fast, or far too small and grows too slowly but I don't know how to meet somewhere in the middle. Scaling in both directions equally at a noticeable yet not obnoxiously large or imperceptible rate.

到目前为止的实现代码:

Implementation code thus far:

// GameObject and GUI assigned in Editor
public GameObject sphere, rotationManager;

// For rotationManager scaling
private float cameraToObject,

void Update()
{
    /* Scale rotationManager based on Cameras distance to object */
    cameraToObject = Vector3.Distance(
        sphere.transform.position, 
        Camera.main.transform.position);

    Debug.Log("distance: " + cameraToObject);

    // If camera isn't too close to our object
    if (cameraToObject > 4f)
    {
        // adjust scale
        rotationManager.transform.localScale = new Vector3(
            cameraToObject / 5, 
            cameraToObject / 5, 
            rotationManager.localScale.z);

        // enable rotationManager
        rotationManager.gameObject.SetActive(true);

    }
    // Camera is too close, turn off GUI
    else
        rotationManager.gameObject.SetActive(false);
}

推荐答案

不太确定自己在做什么,因为用常数除以距离必须产生所需的效果.无论如何,请尝试以下代码:

Not really sure what you are doing wrong, as dividing distance by constant must give required effect. Anyway try this code:

using UnityEngine;

public class AutoSize : MonoBehaviour
{
    public float FixedSize = .005f;
    public Camera Camera;

    void Update ()
    {
        var distance = (Camera.transform.position - transform.position).magnitude;
        var size = distance * FixedSize * Camera.fieldOfView;
        transform.localScale = Vector3.one * size;
        transform.forward = transform.position - Camera.transform.position;
    }
}

它似乎工作正常-预览

另外,也许创建恒定的菜单大小并通过将世界空间转换为屏幕空间来定位它会更加有效.

Also maybe it will be more effective to create constant menu size and just position it by converting world space to the screen space.

这篇关于使物体的视觉尺寸保持恒定,而不管距离如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆