82 lines
2.1 KiB
C#
82 lines
2.1 KiB
C#
using UnityEngine;
|
|
|
|
public class RigidbodyResetter : MonoBehaviour
|
|
{
|
|
private struct RigidbodyState
|
|
{
|
|
public Vector3 position;
|
|
public Quaternion rotation;
|
|
public Vector3 velocity;
|
|
public Vector3 angularVelocity;
|
|
}
|
|
|
|
private RigidbodyState[] initialStates;
|
|
|
|
|
|
|
|
public interface IResettable
|
|
{
|
|
void ResetState();
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
|
|
initialStates = new RigidbodyState[rigidbodies.Length];
|
|
|
|
for (int i = 0; i < rigidbodies.Length; i++)
|
|
{
|
|
initialStates[i] = new RigidbodyState
|
|
{
|
|
position = rigidbodies[i].position,
|
|
rotation = rigidbodies[i].rotation,
|
|
velocity = rigidbodies[i].velocity,
|
|
angularVelocity = rigidbodies[i].angularVelocity
|
|
};
|
|
}
|
|
|
|
ResetAllChildAnimators();
|
|
ResetAllChildScripts();
|
|
|
|
|
|
}
|
|
|
|
public void ResetAndSimulateRigidbodies()
|
|
{
|
|
Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
|
|
|
|
for (int i = 0; i < rigidbodies.Length; i++)
|
|
{
|
|
Rigidbody rb = rigidbodies[i];
|
|
RigidbodyState initialState = initialStates[i];
|
|
|
|
rb.position = initialState.position;
|
|
rb.rotation = initialState.rotation;
|
|
rb.velocity = initialState.velocity;
|
|
rb.angularVelocity = initialState.angularVelocity;
|
|
}
|
|
|
|
ResetAllChildAnimators();
|
|
ResetAllChildScripts();
|
|
}
|
|
|
|
private void ResetAllChildAnimators()
|
|
{
|
|
Animator[] animators = GetComponentsInChildren<Animator>();
|
|
foreach (Animator animator in animators)
|
|
{
|
|
animator.Play("DefaultState", -1, 0f);
|
|
animator.Update(0f);
|
|
}
|
|
}
|
|
|
|
private void ResetAllChildScripts()
|
|
{
|
|
IResettable[] resettables = GetComponentsInChildren<IResettable>();
|
|
foreach (IResettable resettable in resettables)
|
|
{
|
|
resettable.ResetState();
|
|
}
|
|
}
|
|
}
|