craneBeam.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class craneBeam : MonoBehaviour
  5. {
  6. [System.Serializable]
  7. public class LayerInfo
  8. {
  9. public string name;
  10. public string axisName;
  11. public string paramName;
  12. public bool isControlNormalizedTime;
  13. [HideInInspector]
  14. public float curValue;
  15. }
  16. public string[] layersNamesToInitAtHalf;
  17. [SerializeField]
  18. private LayerInfo[] layers;
  19. [SerializeField]
  20. private Animator animator;
  21. void Awake()
  22. {
  23. if (animator == null)
  24. {
  25. animator = GetComponent<Animator>();
  26. }
  27. }
  28. void Start ()
  29. {
  30. Init();
  31. }
  32. void Init()
  33. {
  34. if (layersNamesToInitAtHalf != null)
  35. {
  36. for (int i = 0; i < layersNamesToInitAtHalf.Length; i++)
  37. {
  38. string currentLayerName = layersNamesToInitAtHalf[i];
  39. int currentLayerIndex = animator.GetLayerIndex(currentLayerName);
  40. AnimatorStateInfo curStateInfo = animator.GetCurrentAnimatorStateInfo(currentLayerIndex);
  41. animator.Play(curStateInfo.shortNameHash, currentLayerIndex, 0.5f);
  42. }
  43. }
  44. }
  45. public void Update ()
  46. {
  47. if (layers != null)
  48. {
  49. for (int i = 0; i < layers.Length; i++)
  50. {
  51. LayerInfo curLayerInfo = layers[i];
  52. curLayerInfo.curValue = Input.GetAxis(curLayerInfo.axisName);
  53. if (curLayerInfo.isControlNormalizedTime)
  54. {
  55. int currentLayerIndex = animator.GetLayerIndex(curLayerInfo.name);
  56. AnimatorStateInfo curStateInfo = animator.GetCurrentAnimatorStateInfo(currentLayerIndex);
  57. float normalizedTime = curStateInfo.normalizedTime;
  58. if (normalizedTime > 1 ||
  59. normalizedTime < 0)
  60. {
  61. curLayerInfo.curValue = 0;
  62. animator.Play(curStateInfo.shortNameHash, currentLayerIndex, Mathf.Clamp01(normalizedTime));
  63. }
  64. }
  65. }
  66. }
  67. }
  68. public void FixedUpdate()
  69. {
  70. if (layers != null)
  71. {
  72. for (int i = 0; i < layers.Length; i++)
  73. {
  74. LayerInfo curLayerInfo = layers[i];
  75. animator.SetFloat(curLayerInfo.paramName, curLayerInfo.curValue);
  76. }
  77. }
  78. }
  79. }