Assignment for RMIT Mixed Reality in 2020
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.

59 lines
2.3 KiB

  1. namespace VRTK.Examples.Archery
  2. {
  3. using UnityEngine;
  4. public class ArrowSpawner : MonoBehaviour
  5. {
  6. public GameObject arrowPrefab;
  7. public float spawnDelay = 1f;
  8. private float spawnDelayTimer = 0f;
  9. private BowAim bow;
  10. private void Start()
  11. {
  12. spawnDelayTimer = 0f;
  13. }
  14. private void OnTriggerStay(Collider collider)
  15. {
  16. VRTK_InteractGrab grabbingController = (collider.gameObject.GetComponent<VRTK_InteractGrab>() ? collider.gameObject.GetComponent<VRTK_InteractGrab>() : collider.gameObject.GetComponentInParent<VRTK_InteractGrab>());
  17. if (CanGrab(grabbingController) && NoArrowNotched(grabbingController.gameObject) && Time.time >= spawnDelayTimer)
  18. {
  19. GameObject newArrow = Instantiate(arrowPrefab);
  20. newArrow.name = "ArrowClone";
  21. grabbingController.GetComponent<VRTK_InteractTouch>().ForceTouch(newArrow);
  22. grabbingController.AttemptGrab();
  23. spawnDelayTimer = Time.time + spawnDelay;
  24. }
  25. }
  26. private bool CanGrab(VRTK_InteractGrab grabbingController)
  27. {
  28. return (grabbingController && grabbingController.GetGrabbedObject() == null && grabbingController.IsGrabButtonPressed());
  29. }
  30. private bool NoArrowNotched(GameObject controller)
  31. {
  32. if (VRTK_DeviceFinder.IsControllerLeftHand(controller))
  33. {
  34. GameObject controllerRightHand = VRTK_DeviceFinder.GetControllerRightHand(true);
  35. bow = controllerRightHand.GetComponentInChildren<BowAim>();
  36. if (bow == null)
  37. {
  38. bow = VRTK_DeviceFinder.GetModelAliasController(controllerRightHand).GetComponentInChildren<BowAim>();
  39. }
  40. }
  41. else if (VRTK_DeviceFinder.IsControllerRightHand(controller))
  42. {
  43. GameObject controllerLeftHand = VRTK_DeviceFinder.GetControllerLeftHand(true);
  44. bow = controllerLeftHand.GetComponentInChildren<BowAim>();
  45. if (bow == null)
  46. {
  47. bow = VRTK_DeviceFinder.GetModelAliasController(controllerLeftHand).GetComponentInChildren<BowAim>();
  48. }
  49. }
  50. return (bow == null || !bow.HasArrow());
  51. }
  52. }
  53. }