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.

58 lines
1.7 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class Breakable_Cube : MonoBehaviour
  5. {
  6. private float breakForce = 150f;
  7. private void Start()
  8. {
  9. GetComponent<Rigidbody>().collisionDetectionMode = CollisionDetectionMode.Continuous;
  10. }
  11. private void OnCollisionEnter(Collision collision)
  12. {
  13. var collisionForce = GetCollisionForce(collision);
  14. if (collisionForce > 0)
  15. {
  16. ExplodeCube(collisionForce);
  17. }
  18. }
  19. private float GetCollisionForce(Collision collision)
  20. {
  21. if ((collision.collider.name.Contains("Sword") && collision.collider.GetComponent<Sword>().CollisionForce() > breakForce))
  22. {
  23. return collision.collider.GetComponent<Sword>().CollisionForce() * 1.2f;
  24. }
  25. if (collision.collider.name.Contains("Arrow"))
  26. {
  27. return 500f;
  28. }
  29. return 0f;
  30. }
  31. private void ExplodeCube(float force)
  32. {
  33. foreach (Transform face in GetComponentsInChildren<Transform>())
  34. {
  35. if (face.transform.name == transform.name) continue;
  36. ExplodeFace(face, force);
  37. }
  38. Destroy(gameObject, 10f);
  39. }
  40. private void ExplodeFace(Transform face, float force)
  41. {
  42. face.transform.SetParent(null);
  43. Rigidbody rb = face.gameObject.AddComponent<Rigidbody>();
  44. rb.isKinematic = false;
  45. rb.useGravity = true;
  46. rb.AddExplosionForce(force, Vector3.zero, 0f);
  47. Destroy(face.gameObject, 10f);
  48. }
  49. }
  50. }