创建引力点.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. ///
  6. /// https://blog.csdn.net/tanmengwen/article/details/8698855
  7. /// </summary>
  8. public class CubeHandler : MonoBehaviour
  9. {
  10. private void Start() => Init();
  11. private void Init()
  12. {
  13. Physics.gravity = Vector3.zero;
  14. return;
  15. }
  16. public LayerMask m_MagneticLayers;
  17. public Vector3 m_Position;
  18. public float m_Radius;
  19. public float m_Force;
  20. void FixedUpdate()
  21. {
  22. Collider[] colliders;
  23. Rigidbody rigidbody;
  24. colliders = Physics.OverlapSphere(transform.position + m_Position, m_Radius, m_MagneticLayers);
  25. foreach (Collider collider in colliders)
  26. {
  27. rigidbody = (Rigidbody)collider.gameObject.GetComponent(typeof(Rigidbody));
  28. if (rigidbody == null)
  29. {
  30. continue;
  31. }
  32. rigidbody.AddExplosionForce(m_Force * -1, transform.position + m_Position, m_Radius);
  33. }
  34. }
  35. void OnDrawGizmosSelected()
  36. {
  37. Gizmos.color = Color.red;
  38. Gizmos.DrawWireSphere(transform.position + m_Position, m_Radius);
  39. }
  40. }