ResetInteractionObject.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// Resets an interaction object to it's initial position and rotation after "resetDelay" from interaction trigger.
  7. /// </summary>
  8. public class ResetInteractionObject : MonoBehaviour {
  9. public float resetDelay = 1f; // Time since interaction trigger to reset this Transform
  10. private Vector3 defaultPosition;
  11. private Quaternion defaultRotation;
  12. private Transform defaultParent;
  13. private Rigidbody r;
  14. void Start() {
  15. // Store the defaults
  16. defaultPosition = transform.position;
  17. defaultRotation = transform.rotation;
  18. defaultParent = transform.parent;
  19. r = GetComponent<Rigidbody>();
  20. }
  21. // Called by the Interaction Object
  22. void OnPickUp(Transform t) {
  23. StopAllCoroutines();
  24. StartCoroutine(ResetObject(Time.time + resetDelay));
  25. }
  26. // Reset after a certain delay
  27. private IEnumerator ResetObject(float resetTime) {
  28. while (Time.time < resetTime) yield return null;
  29. var poser = transform.parent.GetComponent<Poser>();
  30. if (poser != null) {
  31. poser.poseRoot = null;
  32. poser.weight = 0f;
  33. }
  34. transform.parent = defaultParent;
  35. transform.position = defaultPosition;
  36. transform.rotation = defaultRotation;
  37. if (r != null) r.isKinematic = false;
  38. }
  39. }
  40. }