Resetting a breakable object

Hello,

I have a breakable object which is just an empty gameobject with several children for the different pieces.
During the game, when needed, the breakable object spawns, explodes and the pieces scatter all over.
I am trying to use an object pool for efficiency, but having trouble resetting the breakable prefab back to its original state in order to be able to explode it again but without having to destroy/instantiate it every time. The breakable stays scattered around.

Is there a way of resetting the prefab back to its original state where the pieces are connected?

Thanks.

2 Answers

2

There is no inbuilt function that does that, I think. So you have to store the positions etc. in an array and reset the values by your self. Try something like this,

var positions : Vector3[];

function Awake() {
   positions = new Vector3[transform.childCount];
   var count : int = 0;

   for (var child : Transform in transform) {
      positions[count] = child.position;
      count++;
   }
}

function ResetObject() {
    var count : int = 0;
    
	for (var child : Transform in transform) {
	   child.position = positions[count];
       count++;
	}
}

You will probably do this with the rotation as well.

Thank you very much for your answer.

I used you solution with a slight change.
Instead of storing the original position, I stored the children’s relative position to the parent and restored that when resetting. I took the rotation from the caller.

Using c#, the code looks like this

	void Start () 
	{
		m_OriginalPosition = new Vector3[transform.childCount];

		for(int i=0; i<transform.childCount; i++)
		{
			Transform child = transform.GetChild(i);
			m_OriginalPosition *= transform.position - child.transform.position;*
  •  }*
    

}

  • public void Reset(Vector3 Vposition, Quaternion QRotation)*
  • {*
  •  for(int i=0; i<transform.childCount; i++)*
    
  •  {*
    
  •  	Transform child = transform.GetChild(i);*
    

child.transform.position = Vposition + m_OriginalPosition*;
_
child.transform.rotation = QRotation;_
_
}*_

* }*
thank you.