c# - 对象没有出现在 Hololens 中 Vuforia 标记的顶部/中心,如何解决?
问题描述
我想在 Vuforia 标记上显示一些对象,但它超出了标记。我想要它在标记的中心。
我使用 Unity Lerp 函数强制对象位于标记的顶部,但它仍然存在一些偏差,并且它会到达标记上的任何位置。
Transform child = transform.GetChild(0);
child.parent = null;
Vector3 _size = GetComponent<ImageTargetBehaviour>().ImageTarget.GetSize();
Vector3 _centre = new Vector3((0 + _size.x) / 2, (0 + _size.y) / 2, (0 + _size.z) / 2);
Vector3 _depth = Vector3.Lerp(child.position, Camera.main.transform.position, 0.72f);
child.position = new Vector3(_depth.x, Mathf.Lerp(_depth.y, _centre.y, 0.03f), _depth.z);
解决方案
看起来这是在标记上执行的。
首先,GetComponent
只打一次电话,其他人只打一次!
// Would be even better if you directly reference this via the Inspector
[SerializeField] private ImageTargetBehaviour itb;
// Alsonthis can probably be done once
[SerializeField] private Transform child;
// And also this should be set once
[SerializeField] private Camera _camera;
private void Awake ()
{
if(!itb) itb = GetComponent<ImageTargetBehaviour>();
child = transform.GetChild(0);
_camera = Camera.main;
}
然后稍后根据您的需要,您可以简单地做
// Depending on your needs it coukd also be + transform.up
child.position = transform.position - transform.forward * XY;
如果您想要它,例如始终在目标前面,与相机位置无关(仅意味着目标面对的方式)。但是,在这种情况下,您可以简单地通过 Inspector 设置一次相对(本地位置),并且它会始终保持此偏移,因为它是一个孩子。
或者例如
// Move XY in the vector position->camera
child.position = transform.position - (transform.position - _camera.transform.position).normalized * XY;
如果您希望它始终“在”目标的“前面”,这意味着更靠近相机,目标取决于相机的位置。
或者干脆
child.position = transform.position;
这将等于
child.localPosition = Vector3.zero;
如果您只是希望它与目标处于相同位置。但是你可以再次通过 Inspector 正确设置它,你应该没问题。
这条线child.parent = null;
打破了这种行为..不确定这是否是有意的,但一旦它不再是孩子,它将来保持在那个位置。此外GetChild(0)
,由于您将孩子移出层次结构,因此您的生产线也会失败......
推荐阅读
- c - 我们如何在c中调用汇编函数?
- kotlin - kotlin 循环遍历 2 个不同长度的列表
- monaco-editor - 在光标位置访问类型定义?
- c# - 如何使用键作为内部列表中的项目初始化字典?
- python - 我有一些事件的实例和该事件的票证实例如何将所有创建的票证实例添加到事件实例列表
- gatsby - 使用 createPage 将组件传递到页面上下文
- terraform - terraform 执行计划中的前缀有什么区别?
- javascript - Javascript else if 语句意外停止执行
- azure - 在 Azure 数据工厂中保存触发器参数以更新插入最近修改的行
- tensorflow2.0 - 在 windows 10 中安装 tensorflow 后出现此错误