Problems with arrays (or how to rewind objects' states)
Hi all,
I'm implementing a mechanic where when triggered, target objects would rewind to their initial positions/rotations/etc. The problematic part of this currently looks something like:
ArrayList Movements = new ArrayList();
ArrayList Rotations = new ArrayList();
public GameObject[] targets;
(in update:)
for (int i = targets.Length-1; i >= 0 ; i--)
{
Movements.Add(targets[i].transform.position);
Rotations.Add(targets[i].transform.rotation);
}
(coroutine to rewind:)
for(int i = 0; i<targets.Length; i++)
{
targets[i].transform.position = (Vector3)Movements[MovementIndex];
Movements.RemoveAt(MovementIndex);
targets[i].transform.rotation = (Quaternion)Rotations[MovementIndex];
Rotations.RemoveAt(MovementIndex);
}
yield return new WaitForSeconds(0f);
I think I've pasted in all the relevant parts for solving what's wrong. Basically, it worked well for just one target, e.g. player, but I want to implement an open array defined in the editor (GameObject[] targets) so I can apply the rewind to any object I want. So when I went and tried it with a player and the camera, it, for some reason (possibly my lack of understand how these arrays are called), it looks like the camera jumps around as if it's using all of the positions and rotations recorded, not just the ones captured from the camera transform. I played around with order of execution in the "for" functions but to no avail..
Any ideas?
Additionally, if anyone has a suggestion on how to implement the captured parameters in a single multi-dimensional array, please share, I couldn't wrap my head around that either. Something like [(player.position1, player.rotation1),(cam.pos1, cam.rot1),....] but that'd be there just to make it neat so is not as important.
you shouldn't have one script with all the targets but this one script on each target. that way every target has it's own arrays and won't iterate all values that have been added (what you're doing right now)
you should also not remove entities during iteration because that reduces the amount of data by half and you skip every second value.
for bundling up the data you need you could create a struct of the values you're saving. they would make the ArrayLists just one consisting of instances of one struct with all the different values saved each frame.
Your answer
Follow this Question
Related Questions
How to compare a position of an object within an array 1 Answer
C# ArrayList Accessing and RemoveAt? 0 Answers
Assigning a different material to different objects 2 Answers
Pass array through text field of game objects,Pass array throug multiple Text fields 0 Answers
How to play a random audio clip from an array in C#? 3 Answers