UserControlInteractions.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// User control demo for Interaction Triggers.
  7. /// </summary>
  8. public class UserControlInteractions : UserControlThirdPerson {
  9. public CharacterThirdPerson character;
  10. public InteractionSystem interactionSystem; // Reference to the InteractionSystem of the character
  11. public bool disableInputInInteraction = true; // If true, will keep the character stopped while an interaction is in progress
  12. public float enableInputAtProgress = 0.8f; // The normalized interaction progress after which the character is able to move again
  13. protected override void Update() {
  14. // Disable input when in interaction
  15. if (disableInputInInteraction && interactionSystem != null && (interactionSystem.inInteraction || interactionSystem.IsPaused())) {
  16. // Get the least interaction progress
  17. float progress = interactionSystem.GetMinActiveProgress();
  18. // Keep the character in place
  19. if (progress > 0f && progress < enableInputAtProgress) {
  20. state.move = Vector3.zero;
  21. state.jump = false;
  22. return;
  23. }
  24. }
  25. // Pass on the FixedUpdate call
  26. base.Update();
  27. }
  28. // Triggering the interactions
  29. void OnGUI() {
  30. // If jumping or falling, do nothing
  31. if (!character.onGround) return;
  32. // If an interaction is paused, resume on user input
  33. if (interactionSystem.IsPaused() && interactionSystem.IsInSync()) {
  34. GUILayout.Label("Press E to resume interaction");
  35. if (Input.GetKey(KeyCode.E)) {
  36. interactionSystem.ResumeAll();
  37. }
  38. return;
  39. }
  40. // If not paused, find the closest InteractionTrigger that the character is in contact with
  41. int closestTriggerIndex = interactionSystem.GetClosestTriggerIndex();
  42. // ...if none found, do nothing
  43. if (closestTriggerIndex == -1) return;
  44. // ...if the effectors associated with the trigger are in interaction, do nothing
  45. if (!interactionSystem.TriggerEffectorsReady(closestTriggerIndex)) return;
  46. // Its OK now to start the trigger
  47. GUILayout.Label("Press E to start interaction");
  48. if (Input.GetKey(KeyCode.E)) {
  49. interactionSystem.TriggerInteraction(closestTriggerIndex, false);
  50. }
  51. }
  52. }
  53. }