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.

49 lines
1.7 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class Sword : VRTK_InteractableObject
  5. {
  6. private float impactMagnifier = 120f;
  7. private float collisionForce = 0f;
  8. private float maxCollisionForce = 4000f;
  9. private VRTK_ControllerReference controllerReference;
  10. public float CollisionForce()
  11. {
  12. return collisionForce;
  13. }
  14. public override void Grabbed(VRTK_InteractGrab grabbingObject)
  15. {
  16. base.Grabbed(grabbingObject);
  17. controllerReference = VRTK_ControllerReference.GetControllerReference(grabbingObject.controllerEvents.gameObject);
  18. }
  19. public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject)
  20. {
  21. base.Ungrabbed(previousGrabbingObject);
  22. controllerReference = null;
  23. }
  24. protected override void OnEnable()
  25. {
  26. base.OnEnable();
  27. controllerReference = null;
  28. interactableRigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
  29. }
  30. private void OnCollisionEnter(Collision collision)
  31. {
  32. if (VRTK_ControllerReference.IsValid(controllerReference) && IsGrabbed())
  33. {
  34. collisionForce = VRTK_DeviceFinder.GetControllerVelocity(controllerReference).magnitude * impactMagnifier;
  35. var hapticStrength = collisionForce / maxCollisionForce;
  36. VRTK_ControllerHaptics.TriggerHapticPulse(controllerReference, hapticStrength, 0.5f, 0.01f);
  37. }
  38. else
  39. {
  40. collisionForce = collision.relativeVelocity.magnitude * impactMagnifier;
  41. }
  42. }
  43. }
  44. }