I’m trying to record the position and rotation of all the children of an object on Start. Then on Update, if the player hits the reset button, the children are reset to their original positions and rotations.
I’m trying to accomplish this using arrays but A) my arrays don’t seem to be working and B) I don’t even know if that is the correct way to approach this.
Here’s what I’ve got:
public GameObject grabbableObject;
private Vector3[] originalPostions;
private Quaternion[] originalRotations;
void Start () {
numParts = grabbableObject.transform.childCount;
for (int i = 0; i < numParts; i++) {
originalPostions[i] = grabbableObject.transform.GetChild(i).gameObject.transform.position;
originalRotations [i] = grabbableObject.transform.GetChild (i).gameObject.transform.rotation;
}
}
void Update () {
if ([reset button down]) {
(int i = 0; i < numParts; i++) {
grabbableObject.transform.GetChild (i).transform.position = originalPostions [i];
grabbableObject.transform.GetChild (i).transform.rotation = originalRotations [i];
}
}
}
I appreciate the help!
I would use a List myself;
public GameObject grabbableObject;
private List<Vector3> originalPostions = new List<Vector3>();
private List<Quaternion> originalRotations = new List<Quaternion>();
private int numParts;
void Start()
{
numParts = grabbableObject.transform.childCount;
for (int i = 0; i < numParts; i++)
{
originalPostions.Add(grabbableObject.transform.GetChild(i).gameObject.transform.position);
originalRotations.Add(grabbableObject.transform.GetChild(i).gameObject.transform.rotation);
}
}
void Update()
{
if ([reset button down])
{
for (int i = 0; i < numParts; i++) {
grabbableObject.transform.GetChild(i).transform.position = originalPostions[i];
grabbableObject.transform.GetChild(i).transform.rotation = originalRotations[i];
}
}
}
You will need this for the list;
using System.Collections.Generic;
I’d say a Dictionary so that you can easily find the position of an arbitrary given object, using the object itself as your key. This also
Further, I’d say you should create a class containing both position and rotation, and use that in your Dictionary. Parallel arrays are almost never a good idea - far too easy for them to get out of sync and break everything.
With this approach, all of your data will be in a single, neatly organized data structure.
public class TransformData {
public TransformData(Vector3 p, Quaternion r) {
position = p;
rotation = r;
}
public Vector3 position;
public Quaternion rotation;
}
public Dictionary<GameObject, TransformData> originals = new Dictionary<GameObject, TransformData>();
void Start() {
numParts = grabbableObject.transform.childCount;
for (int i = 0; i < numParts; i++)
{
GameObject thisObj = grabbableObject.transform.GetChild(i).gameObject;
originals.Add(thisObj, new TransformData(thisObj.transform.position, thisObj..transform.rotation) );
}
}
void Update() {
if (button pressed) {
numParts = grabbableObject.transform.childCount;
for (int i = 0; i < numParts; i++)
{
GameObject thisObj = grabbableObject.transform.GetChild(i).gameObject;
thisObj.transform.position = originals[thisObj].position;
thisObj.transform.rotation = originals[thisObj].rotation;
}
}
}
1 Like
That’s 2 ciders I owe you, StarManta.
Thanks to you, too, WarmedxMints. According to my Debug logs, that was looking like it was going to work. Then I saw StarManta’s post and headed in that direction.
Question: is there a way to mark an answer as the “Correct” answer?
I think you can only do that in Unity Answers, not here 