Log in or register to post comments

Objects following Camera after spawning objects in the world

January 15, 2019 - 4:37am #1

Hello community,

My setup:

  • Microsoft HoloLens
  • Vuforia 8.0.10 (Track Device Pose & Tracking Mode Positional)
  • Unity 2018.3.0f2

My problem:

I use the default image target of vuforia (astronaut) to determine the position of the ImageTarget in the world. This works very well so far. But once I spawned an object at the position of the detected ImageTarget, after vuforia recognized it, the spawned objects are somehow bound to the camera.

This means that when I move around in the room, the spawned object moves with me instead of staying where I spawned it in the room.

When I spawn the object BEFORE vuforia detects the ImageTarget, the object is correctly placed in the room and I can move around it.

BUT: If the ImageTarget is detected and I reposition the object, it will be broken and follows the camera.

Also when I enter a constant vector for the object when the ImageTarget gets detected it also works.

public class TrackableHandler : MonoBehaviour, ITrackableEventHandler
{
    private bool found;
    private bool spawned;
    private TrackableBehaviour mTrackableBehaviour;
    public GameObject spawnObject;

    private void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }

    private void OnDestroy()
    {
        if (mTrackableBehaviour)
            mTrackableBehaviour.UnregisterTrackableEventHandler(this);
    }

    public void OnTrackableStateChanged(
        TrackableBehaviour.Status previousStatus,
        TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            OnTrackingFound();
            found = true;
        }
        else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
                 newStatus == TrackableBehaviour.Status.NO_POSE)
        {
            found = false;
        }
        else
        {
            found = false;
        }
    }

    private void OnTrackingFound()
    {
        if (!spawned)
        {
            GameObject imageTarget = GameObject.Find("ImageTarget");
            Vector3 position = imageTarget.transform.position;
// spawning with a constant position works: // position = new Vector3(1,2,3);
            GameObject instantiate = Instantiate(spawnObject, position, Quaternion.identity);

            spawned = true;
        }
    }
}

Any ideas on that?

Greetings

Objects following Camera after spawning objects in the world

June 1, 2019 - 3:19pm #4

Apparently the parent of your instantiated object becomes the Camera. Strange, because your code seems fine. You should debug further to find out what is going on. Suggestions:

  • Test first in Unity (using laptop Camera) instead of HoloLens to see what the parent of the instantiated object actually is in the Hierarchy.
  • Add an explicit Parent to the Instantiate call.
  • Just to be sure: Check that the prefab selected in the Inspector is pointing to the Assets and not to another object in the Hierarchy. Should not make much difference though, because according to Unity documentation, Instantiate without a Parent will always instantiate the object in the root.

Objects following Camera after spawning objects in the world

May 18, 2019 - 7:18pm #3

same problem as yours ,and could't find any solution in google .if u find some way to fix please reply me bro,thx!

 

Objects following Camera after spawning objects in the world

January 16, 2019 - 11:05pm #2

Try to set a worldanchor to the spawned object so it stays at the location where it is supposed to be (you have to remove the anchor if you want to update position or rotation).

GameObject.Find() could return an object that is not intended (and it is slow) for example Vuforia will spawn dummy objects for each marker in the dataset. Try to set a reference to the image target.

Log in or register to post comments