unity-game-enginegame-physicsmrtk

MRTK - Line up gameObjects depending on rotation of Parent


I implement a MRTK application. I want to line up N gameobjects next to a parent gameobject if i click on a button.

the child-gameobject get created.

The problem is quite sure the calculation of "position" of my code-snippet.

var i = 0;
foreach(var instance in instances) {
 var position = transform.position + 0.33f * i * Vector3.right;
 var forward = transform.forward;
 forward.y = Mathf.Clamp(forward.y, 0f, 0.5f);
 forward.Normalize();
 var rotation = Quaternion.LookRotation(forward, Vector3.up);
 var presenter = Instantiate(prefab, position, rotation,  
                 MixedRealityPlaySpace.Instance.transform).GetComponent<Presenter>();
  i++; 
}                

at the moment every gameobject is right to the parent. If i rotate the Parent by 90 degrees around Y the Gameobjects should not appear right to the parent. Instead they should come forward.

I know the rotation-Value of the parent, but i don't know hot to calculate the value i replace Vector3.right with.

I tried multiple ways of calculating the vector3 value, but nothing really worked out for me.


Solution

  • Vector3.right returns the global vector pointing right (along the x-axis). If I am not mistaken, to achieve what you want, all you need to do is take the local right vector, meaning transform.right. This assumes that the script is attached to your parent object. Otherwise, just use a reference to it and apply the same principle (parentObj.transform.right).