请参阅视觉辅助。在gif之后,有两个GUI。左侧的GUI根据摄像机到对象的距离正确缩放。右侧的GUI是缩放,在“场景”中清晰可见。视图,但在游戏视图中甚至不明显。该应用程序用于Hololens,因为用户头部控制Unity场景中的摄像头。
问题在于设置对象缩放。我整天都试图复制左边GUI上显示的缩放类型。划分距离并将其作为缩放因子显然是不正确的,因为物体要么太大而且生长得太快,要么太小而且生长得太慢但是我不知道如何在某处遇到中间。在两个方向上均衡地以明显但不令人讨厌的大或不易察觉的速度进行缩放。
到目前为止的实施代码:
// 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);
}
答案 0 :(得分:1)
不确定你做错了什么,因为将距离除以常数必须给出所需的效果。无论如何尝试这段代码:
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;
}
}
似乎工作正常 - preview
也许创建常量菜单大小并通过将世界空间转换为屏幕空间来定位它会更有效。