How to get all child objects back to its start position.
Hi everyone!
I am developing AR app using Unity 2019.1.0f.2 and Vuforia SDK and I came across annoying problem.
I need to reset position of my main object (model of an camera) to its starting position. So I wrote simple functions to do that:
PositionReset.cs
private static PositionReset instance;
public static PositionReset Instance
{
get { return instance ?? (instance = GameObject.FindObjectOfType(typeof(PositionReset)) as PositionReset); }
}
/// <summary>
/// main 3D object instance
/// </summary>
public GameObject mainObject;
/// <summary>
/// hold original position of all objects
/// </summary>
private Dictionary<string,Vector3> startPosition = new Dictionary<string, Vector3>();
public void Awake()
{
if (instance == null)
instance = this;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ResetPosition()
{
mainObject = GameObject.Find("193.0-0 Ognioszczelna Kamera Cyfrowa typu OKA-1_C 1");
foreach (Transform transform in mainObject.GetComponentsInChildren<Transform>())
{
Vector3 localPosition;
if (startPosition.TryGetValue(transform.name, out localPosition))
{
Debug.Log("RESET: Name: " + transform.name + " Local position: " + localPosition);
transform.position = localPosition;
//transform.position.Set(localPosition.x,localPosition.y,localPosition.z);
}
}
}
public void GetStartPosition()
{
//startPosition
//Transform[] childTransform = GetComponentsInChildren<Transform>();
mainObject = GameObject.Find("193.0-0 Ognioszczelna Kamera Cyfrowa typu OKA-1_C 1");
if (startPosition.Count < 1)
{
foreach (Transform transform in mainObject.GetComponentsInChildren<Transform>())
{
Debug.Log("ADD, Name: " + transform.name + " Local position: " + transform.localPosition);
startPosition.Add(transform.name, transform.position);
}
}
}
I use function GetStartPosition() on imagetarget detection in another class, and then with button click from UI want to reset position with function ResetPosition(). What I get in the end is that all objects end up in random places. What am I missing? How does Unity take the position of those objects? in my opinion they should come back to starting point that I grabbed with GetStartPosition().
Here are some screenshots from scene view. Starting point(which should be end point) and actual messed result.
I would appreciate any help. Thanks, Simon
Answer by brarife · Sep 03, 2019 at 03:03 PM
Add the coradnites of its stars pos to the script for reset