TouchRigidbodyExplosion.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace VoxelImporter
  5. {
  6. public class TouchRigidbodyExplosion : MonoBehaviour
  7. {
  8. public float radius = 10f;
  9. public float power = 500f;
  10. void Update()
  11. {
  12. bool explosion = false;
  13. Vector3 position = Vector3.zero;
  14. if (Input.GetMouseButtonDown(0))
  15. {
  16. explosion = true;
  17. position = Input.mousePosition;
  18. }
  19. if (Input.touchCount > 0)
  20. {
  21. explosion = true;
  22. position = Input.GetTouch(0).position;
  23. }
  24. if (explosion)
  25. {
  26. Ray ray = Camera.main.ScreenPointToRay(position);
  27. RaycastHit hit;
  28. if (Physics.Raycast(ray, out hit, 1000))
  29. {
  30. var colliders = Physics.OverlapSphere(hit.point, radius);
  31. for (int i = 0; i < colliders.Length; i++)
  32. {
  33. var rigidbody = colliders[i].GetComponent<Rigidbody>();
  34. if (rigidbody == null) continue;
  35. rigidbody.isKinematic = false;
  36. rigidbody.AddExplosionForce(power, hit.point, radius);
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }