1 (edited by digitalrock 2022-01-14 14:53:12)

Topic: Rotate tileset at center AND follow game object

Hi Alex, I have the following hierarchy:

RotationParent (compass rotation script)
   -- Tileset Map (follow AR camera script)

The rotation parent setup allows the compass to rotate the tileset map at its center.  This works great.

I use the FollowGameObject script on the tileset to have the map follow my AR camera.  This works great too, but only if the compass rotation script isn't enabled; otherwise the map will jump out of position AND leaves the parent gameobject behind.

My problem is I can't figure out how to get the rotation of the map (at its center) from the compass AND the map follow the AR camera working together.  As I said these two features work alone, but not together.

I tried attaching the followGameObject script to the RotationParent instead of the tileset map, but results were weird.

I tried adding another parent (for the follow script) using a hierarchy like this:

PositionParent (follow AR camera script)
-- RotationParent (compass rotation script)
   -- Tileset Map

...with the following modification to the followGameObject script:

// assign pos
  //map.transform.position = pos;
  // try this
  transform.position = pos;

but I'm sure you can imagine nothing I do is working, including this hierarchy.  Things come close though. 

Thought I'd see if you could make a suggestion on how to wire this up.  Hopefully you can make sense of what I'm trying to do.

Regards.

Re: Rotate tileset at center AND follow game object

Doing some additional testing and I'm super close.  With this hierarchy:

PositionParent (follow AR camera script)
-- RotationParent (compass rotation script)
   -- Tileset Map

...and this code:

/// <summary>
        /// Updates map position
        /// </summary>
        private void UpdateMap()
        {
            // Store last position of GameObject
            lastPosition = target.transform.position;

            // Size of map in scene
            Vector2 size = control.sizeInScene;

            // Calculate offset (in tile position)
            Vector3 offset = lastPosition - transform.position;
            offset.x = offset.x / OnlineMapsUtils.tileSize / size.x * map.width * map.zoomCoof;
            offset.z = offset.z / OnlineMapsUtils.tileSize / size.y * map.height * map.zoomCoof;

            // Calculate current tile position of the center of the map
            tx -= offset.x;
            ty += offset.z;

            if (Mathf.Abs(offset.x) > float.Epsilon || Mathf.Abs(offset.z) > float.Epsilon) map.SetTilePosition(tx, ty);

            // Update map GameObject position
            var pos = lastPosition;

            // assign pos
            transform.position = pos;
        }

the above seems to work the best so far, only problem being that the tiles/map center moves a little different than what should occur...

It's as if the offset should be calculated with respect to the current compass rotation of the map.  I've notice if I hard-code a ZERO where the compass setting rotates the map, then the map/tiles/ar following all work exactly as expected.  With compass angle driving the rotation, this causes the follow script calculations (map.setTilePosition(tx,ty)) to be off comparable/correlated to the angle of rotation of the tileset parent (rotation parent).

Re: Rotate tileset at center AND follow game object

Hello.

In your case, FollowGameObject should be located on the parent GO and UpdateMap method should look like this:

private void UpdateMap()
{
    // Store last position of GameObject
    lastPosition = target.transform.position;

    // Size of map in scene
    Vector2 size = control.sizeInScene;

    // Calculate offset (in tile position)
    Vector3 offset = Quaternion.Inverse(transform.rotation) * (lastPosition - transform.position);
    offset.x = offset.x / OnlineMapsUtils.tileSize / size.x * map.width * map.zoomCoof;
    offset.z = offset.z / OnlineMapsUtils.tileSize / size.y * map.height * map.zoomCoof;

    // Calculate current tile position of the center of the map
    tx -= offset.x;
    ty += offset.z;

    // Set position of the map center
    if (Mathf.Abs(offset.x) > float.Epsilon || Mathf.Abs(offset.z) > float.Epsilon) map.SetTilePosition(tx, ty);

    // Update map GameObject position
    transform.position = lastPosition;
}

P.S. In your case, you don't need two parents. One is enough.

Kind Regards,
Infinity Code Team.

uContext is Editor PowerPack (more than 60 tools) that take your workflow in Unity Editor to a next level, simplifies working with content, adds new features, corrects and improves the editor’s built-in behaviors. Click here to download uContext Basic for free.

Re: Rotate tileset at center AND follow game object

Thanks Alex, worked amazing.

P.S. In your case, you don't need two parents. One is enough.

I actually would like to use this extra parent hierarchy as I have a sibling which needs to follow along, but not rotate with the tileset map.  I figured it would work just fine with the extra parent, but it turns out the solution you provide will only work with the one parent setup.  Can you explain how I'd setup the UpdateMap function for this?

Thanks again Alex, super awesome support.

5 (edited by digitalrock 2022-01-14 20:41:48)

Re: Rotate tileset at center AND follow game object

digitalrock wrote:

...
I actually would like to use this extra parent hierarchy as I have a sibling which needs to follow along, but not rotate with the tileset map.  I figured it would work just fine with the extra parent, but it turns out the solution you provide will only work with the one parent setup.  Can you explain how I'd setup the UpdateMap function for this?...

I've solved what I need here using a different structure, just need to test a little more.  In any case, the solution you provided is perfect.