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.

68 lines
2.8 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Effects
  4. {
  5. [RequireComponent(typeof (SphereCollider))]
  6. public class AfterburnerPhysicsForce : MonoBehaviour
  7. {
  8. public float effectAngle = 15;
  9. public float effectWidth = 1;
  10. public float effectDistance = 10;
  11. public float force = 10;
  12. private Collider[] m_Cols;
  13. private SphereCollider m_Sphere;
  14. private void OnEnable()
  15. {
  16. m_Sphere = (GetComponent<Collider>() as SphereCollider);
  17. }
  18. private void FixedUpdate()
  19. {
  20. m_Cols = Physics.OverlapSphere(transform.position + m_Sphere.center, m_Sphere.radius);
  21. for (int n = 0; n < m_Cols.Length; ++n)
  22. {
  23. if (m_Cols[n].attachedRigidbody != null)
  24. {
  25. Vector3 localPos = transform.InverseTransformPoint(m_Cols[n].transform.position);
  26. localPos = Vector3.MoveTowards(localPos, new Vector3(0, 0, localPos.z), effectWidth*0.5f);
  27. float angle = Mathf.Abs(Mathf.Atan2(localPos.x, localPos.z)*Mathf.Rad2Deg);
  28. float falloff = Mathf.InverseLerp(effectDistance, 0, localPos.magnitude);
  29. falloff *= Mathf.InverseLerp(effectAngle, 0, angle);
  30. Vector3 delta = m_Cols[n].transform.position - transform.position;
  31. m_Cols[n].attachedRigidbody.AddForceAtPosition(delta.normalized*force*falloff,
  32. Vector3.Lerp(m_Cols[n].transform.position,
  33. transform.TransformPoint(0, 0, localPos.z),
  34. 0.1f));
  35. }
  36. }
  37. }
  38. private void OnDrawGizmosSelected()
  39. {
  40. //check for editor time simulation to avoid null ref
  41. if(m_Sphere == null)
  42. m_Sphere = (GetComponent<Collider>() as SphereCollider);
  43. m_Sphere.radius = effectDistance*.5f;
  44. m_Sphere.center = new Vector3(0, 0, effectDistance*.5f);
  45. var directions = new Vector3[] {Vector3.up, -Vector3.up, Vector3.right, -Vector3.right};
  46. var perpDirections = new Vector3[] {-Vector3.right, Vector3.right, Vector3.up, -Vector3.up};
  47. Gizmos.color = new Color(0, 1, 0, 0.5f);
  48. for (int n = 0; n < 4; ++n)
  49. {
  50. Vector3 origin = transform.position + transform.rotation*directions[n]*effectWidth*0.5f;
  51. Vector3 direction =
  52. transform.TransformDirection(Quaternion.AngleAxis(effectAngle, perpDirections[n])*Vector3.forward);
  53. Gizmos.DrawLine(origin, origin + direction*m_Sphere.radius*2);
  54. }
  55. }
  56. }
  57. }