SpringManager.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. //SpingManager.cs for unity-chan!
  3. //
  4. //Original Script is here:
  5. //ricopin / SpingManager.cs
  6. //Rocket Jump : http://rocketjump.skr.jp/unity3d/109/
  7. //https://twitter.com/ricopin416
  8. //
  9. //Revised by N.Kobayashi 2014/06/24
  10. // Y.Ebata
  11. //
  12. using UnityEngine;
  13. using System.Collections;
  14. namespace UnityChan
  15. {
  16. public class SpringManager : MonoBehaviour
  17. {
  18. //Kobayashi
  19. // DynamicRatio is paramater for activated level of dynamic animation
  20. public float dynamicRatio = 1.0f;
  21. //Ebata
  22. public float stiffnessForce;
  23. public AnimationCurve stiffnessCurve;
  24. public float dragForce;
  25. public AnimationCurve dragCurve;
  26. public SpringBone[] springBones;
  27. void Start ()
  28. {
  29. UpdateParameters ();
  30. }
  31. void Update ()
  32. {
  33. #if UNITY_EDITOR
  34. //Kobayashi
  35. if(dynamicRatio >= 1.0f)
  36. dynamicRatio = 1.0f;
  37. else if(dynamicRatio <= 0.0f)
  38. dynamicRatio = 0.0f;
  39. //Ebata
  40. UpdateParameters();
  41. #endif
  42. }
  43. private void LateUpdate ()
  44. {
  45. //Kobayashi
  46. if (dynamicRatio != 0.0f) {
  47. for (int i = 0; i < springBones.Length; i++) {
  48. if (dynamicRatio > springBones [i].threshold) {
  49. springBones [i].UpdateSpring ();
  50. }
  51. }
  52. }
  53. }
  54. private void UpdateParameters ()
  55. {
  56. UpdateParameter ("stiffnessForce", stiffnessForce, stiffnessCurve);
  57. UpdateParameter ("dragForce", dragForce, dragCurve);
  58. }
  59. private void UpdateParameter (string fieldName, float baseValue, AnimationCurve curve)
  60. {
  61. var start = curve.keys [0].time;
  62. var end = curve.keys [curve.length - 1].time;
  63. //var step = (end - start) / (springBones.Length - 1);
  64. var prop = springBones [0].GetType ().GetField (fieldName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
  65. for (int i = 0; i < springBones.Length; i++) {
  66. //Kobayashi
  67. if (!springBones [i].isUseEachBoneForceSettings) {
  68. var scale = curve.Evaluate (start + (end - start) * i / (springBones.Length - 1));
  69. prop.SetValue (springBones [i], baseValue * scale);
  70. }
  71. }
  72. }
  73. }
  74. }