You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UnityStandardAssets.Utility
  6. {
  7. public class ObjectResetter : MonoBehaviour
  8. {
  9. private Vector3 originalPosition;
  10. private Quaternion originalRotation;
  11. private List<Transform> originalStructure;
  12. private Rigidbody Rigidbody;
  13. // Use this for initialization
  14. private void Start()
  15. {
  16. originalStructure = new List<Transform>(GetComponentsInChildren<Transform>());
  17. originalPosition = transform.position;
  18. originalRotation = transform.rotation;
  19. Rigidbody = GetComponent<Rigidbody>();
  20. }
  21. public void DelayedReset(float delay)
  22. {
  23. StartCoroutine(ResetCoroutine(delay));
  24. }
  25. public IEnumerator ResetCoroutine(float delay)
  26. {
  27. yield return new WaitForSeconds(delay);
  28. // remove any gameobjects added (fire, skid trails, etc)
  29. foreach (var t in GetComponentsInChildren<Transform>())
  30. {
  31. if (!originalStructure.Contains(t))
  32. {
  33. t.parent = null;
  34. }
  35. }
  36. transform.position = originalPosition;
  37. transform.rotation = originalRotation;
  38. if (Rigidbody)
  39. {
  40. Rigidbody.velocity = Vector3.zero;
  41. Rigidbody.angularVelocity = Vector3.zero;
  42. }
  43. SendMessage("Reset");
  44. }
  45. }
  46. }