Grenade.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Valve.VR.InteractionSystem.Sample
  4. {
  5. public class Grenade : MonoBehaviour
  6. {
  7. public GameObject explodePartPrefab;
  8. public int explodeCount = 10;
  9. public float minMagnitudeToExplode = 1f;
  10. private Interactable interactable;
  11. private void Start()
  12. {
  13. interactable = this.GetComponent<Interactable>();
  14. }
  15. private void OnCollisionEnter(Collision collision)
  16. {
  17. if (interactable != null && interactable.attachedToHand != null) //don't explode in hand
  18. return;
  19. if (collision.impulse.magnitude > minMagnitudeToExplode)
  20. {
  21. for (int explodeIndex = 0; explodeIndex < explodeCount; explodeIndex++)
  22. {
  23. GameObject explodePart = (GameObject)GameObject.Instantiate(explodePartPrefab, this.transform.position, this.transform.rotation);
  24. explodePart.GetComponentInChildren<MeshRenderer>().material.SetColor("_TintColor", Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f));
  25. }
  26. Destroy(this.gameObject);
  27. }
  28. }
  29. }
  30. }