MegaFlowRBody.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. [AddComponentMenu("MegaFlow/RigidBody Control")]
  3. [ExecuteInEditMode]
  4. public class MegaFlowRBody : MegaFlowEffect
  5. {
  6. //Matrix4x4 tm;
  7. //Matrix4x4 invtm;
  8. float coef = 0.0f;
  9. Rigidbody rbody;
  10. [ContextMenu("Help")]
  11. public void RbodyHelp()
  12. {
  13. Application.OpenURL("http://www.west-racing.com/mf/?page_id=5898");
  14. }
  15. void Start()
  16. {
  17. rbody = gameObject.GetComponent<Rigidbody>();
  18. }
  19. void Update()
  20. {
  21. if ( source && source.frames.Count > 0 )
  22. {
  23. scl = source.Scale * scale;
  24. source.Prepare();
  25. framenum = Mathf.Clamp(framenum, 0, source.frames.Count - 1);
  26. frame = source.frames[framenum];
  27. //Matrix4x4 offtm = Matrix4x4.TRS(source.flow.size * 0.5f, Quaternion.identity, Vector3.one);
  28. //Matrix4x4 offtm = Matrix4x4.TRS((frame.size * 0.5f) + frame.offset, Quaternion.identity, Vector3.one);
  29. //tm = offtm * source.transform.worldToLocalMatrix;
  30. //invtm = tm.inverse;
  31. coef = source.Density * Area * Mathf.Pow(source.Reynolds, -0.5f);
  32. }
  33. }
  34. void FixedUpdate()
  35. {
  36. if ( rbody )
  37. {
  38. bool inbounds = true;
  39. //Vector3 airvel = tm.MultiplyVector(frame.GetGridVel(invtm.MultiplyPoint3x4(rbody.position), ref inbounds) * scl);
  40. Vector3 airvel = frame.GetGridVelWorld(rbody.position, ref inbounds) * scl;
  41. if ( inbounds )
  42. {
  43. Vector3 tvel = (airvel * scale) - rbody.velocity;
  44. rbody.AddForce(tvel.normalized * coef * tvel.magnitude);
  45. }
  46. }
  47. }
  48. }