在 Image Target 渲染之前,具有重力的物体会不断下落
Objects with gravity fall continuously before Image Target renders
我正在尝试将一些内容渲染到图像目标上,其中一个对象具有 Rigidbody
重力。有重力的物体在场景一开始就开始下落,但是图像目标还没有被识别,所以一直下落。
我在网上看到一些代码建议实施 ITrackableEventHandler
,但 Vuforia 网站上的 guide 不再有效。
Vuforia 建议的代码如下:
using UnityEngine;
using Vuforia;
using System.Collections;
public class MyPrefabInstantiator : MonoBehaviour, ITrackableEventHandler {
private TrackableBehaviour mTrackableBehaviour;
public Transform myModelPrefab;
// Use this for initialization
void Start ()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour) {
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
// Update is called once per frame
void Update ()
{
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
}
private void OnTrackingFound()
{
if (myModelPrefab != null)
{
Transform myModelTrf = GameObject.Instantiate(myModelPrefab) as Transform;
myModelTrf.parent = mTrackableBehaviour.transform;
myModelTrf.localPosition = new Vector3(0f, 0f, 0f);
myModelTrf.localRotation = Quaternion.identity;
myModelTrf.localScale = new Vector3(0.0005f, 0.0005f, 0.0005f);
myModelTrf.gameObject.active = true;
}
}
}
Vuforia 已通过 class DefaultTrackableEventHandler
为您实施 ITrackableEventHandler
您可以使用如下脚本启用和禁用重力:
using UnityEngine;
public class EnablePlayerGravity : DefaultTrackableEventHandler
{
public Rigidbody player;
override protected void OnTrackingLost()
{
player.useGravity = false;
}
override protected void OnTrackingFound()
{
player.useGravity = true;
}
}
您不需要使用 ImageTarget
的 TrackableBehavior
注册它,因为 DefaultTrackableEventHandler
的 Start
方法已经为您完成了。您需要做的就是将此脚本放到 ImageTarget
上,然后在检查器中设置玩家(或任何具有重力的物体)的刚体。
我正在尝试将一些内容渲染到图像目标上,其中一个对象具有 Rigidbody
重力。有重力的物体在场景一开始就开始下落,但是图像目标还没有被识别,所以一直下落。
我在网上看到一些代码建议实施 ITrackableEventHandler
,但 Vuforia 网站上的 guide 不再有效。
Vuforia 建议的代码如下:
using UnityEngine;
using Vuforia;
using System.Collections;
public class MyPrefabInstantiator : MonoBehaviour, ITrackableEventHandler {
private TrackableBehaviour mTrackableBehaviour;
public Transform myModelPrefab;
// Use this for initialization
void Start ()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour) {
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
// Update is called once per frame
void Update ()
{
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
}
private void OnTrackingFound()
{
if (myModelPrefab != null)
{
Transform myModelTrf = GameObject.Instantiate(myModelPrefab) as Transform;
myModelTrf.parent = mTrackableBehaviour.transform;
myModelTrf.localPosition = new Vector3(0f, 0f, 0f);
myModelTrf.localRotation = Quaternion.identity;
myModelTrf.localScale = new Vector3(0.0005f, 0.0005f, 0.0005f);
myModelTrf.gameObject.active = true;
}
}
}
Vuforia 已通过 class DefaultTrackableEventHandler
ITrackableEventHandler
您可以使用如下脚本启用和禁用重力:
using UnityEngine;
public class EnablePlayerGravity : DefaultTrackableEventHandler
{
public Rigidbody player;
override protected void OnTrackingLost()
{
player.useGravity = false;
}
override protected void OnTrackingFound()
{
player.useGravity = true;
}
}
您不需要使用 ImageTarget
的 TrackableBehavior
注册它,因为 DefaultTrackableEventHandler
的 Start
方法已经为您完成了。您需要做的就是将此脚本放到 ImageTarget
上,然后在检查器中设置玩家(或任何具有重力的物体)的刚体。