Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Instantiated object not positioned correctly when using World Anchors

bfichterbfichter
edited October 2016 in Questions And Answers

I will try to explain this the best I can.

I want to instantiate and position a GameObject at a specific location relative to an existing GameObject when using WorldAnchor. Both GameObjects should be under the same parent.

Here's what I am doing now (which isn't working):
I have a GameObject (HologramCollection) I am using as a parent for a collection of models.
I have developed controls to finely position this HologramCollection. (Basically buttons to move .01m a click.)
I add a WorldAnchor component to this HologramCollection upon start.
Each time I move the HologramCollection I am destroying then adding the WorldAnchor again:

DestroyImmediate(hologram.GetComponent<WorldAnchor>());
hologram.transform.position = newLocation;
anchor = hologram.AddComponent<WorldAnchor>();

This all works fine.

My application receives coordinates externally (lat, lon).
When I get the new coordinate, I convert it to the local space of one of the children models of the HologramCollection.

I convert the local position to the Unity world position using:

terrainModel.transform.TransformPoint(new Vector3(x, y, z))

I instantiate an object at that position.

This all works fine and the object is placed correctly when I don't create or set a WorldAnchor.

As soon as I use world anchors, the instantiated object is not positioned correctly. Furthermore if I move the Hololens a lot, the position is different each time.

I am wondering if the position of the WorldAnchor should be accounted for or if I am missing something else.

Answers

  • Options
    bfichterbfichter
    edited October 2016

    Any thoughts?

  • Options
    timgtimg mod
    edited October 2016

    Hi @bfichter

    I've been playing around with this and haven't been able to reproduce what you are describing; I get the same behavior with and without WorldAnchors.

    How are you Instantiating your new object?

    I created a scene with two cubes (red and blue) with red on the left and blue on the right. Both cubes are children of the same HologramCollection GameObject. When I move my collection, I remove the WorldAnchor

    public void MoveCollection()
    {
        isMoving = true;
        if (anchor)
        {
            DestroyImmediate(anchor);
        }
        SpatialMappingManager.Instance.DrawVisualMeshes = true;
    }
    

    When I stop moving my collection, I create and add a new WorldAnchor

    public void StopMoving()
    {
        isMoving = false;
        anchor = gameObject.AddComponent<WorldAnchor>();
        SpatialMappingManager.Instance.DrawVisualMeshes = false;
    }
    

    When I add a new object (green cube), I position it to the left of the red cube. Successively added green cubes are positioned to the left of previously added green cubes.

    float xOffset = 0.0f;
    public void CreateObject()
    {
        Vector3 newLocation = leftCube.transform.TransformPoint(
            xOffset -= 1.0f,
            leftCube.transform.localPosition.y,
            leftCube.transform.localPosition.z);
        Instantiate(prefab, newLocation, transform.rotation, transform);
    }
    

    All code is in the same script which is attached to my HologramCollection object.

    Regardless of where I position the HologramPosition, the green cubes always appear where expected (in a line to the left of the red cube). I see the same behavior with and without adding the WorldAnchor (in StopMoving() above).

    One additional note, I'm using Unity 5.5.0b7

Sign In or Register to comment.