GeartoComb.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GeartoComb : MonoBehaviour
  5. {
  6. public GearType2 parentGear;
  7. public GameObject Comb;
  8. [HideInInspector]
  9. public bool Comb_Move_up=true;
  10. [HideInInspector]
  11. public float Max_Height;
  12. [HideInInspector]
  13. public float Comb_Total_Movement;
  14. [HideInInspector]
  15. public float time = 0f;
  16. RaycastHit hit;
  17. Ray ray;
  18. [HideInInspector]
  19. public GameObject current;
  20. int layerMask = 1 << 8;
  21. public float Radius_of_Gear;
  22. float Speed;
  23. // Start is called before the first frame update
  24. float Default_Y_Location;
  25. float Ratio;
  26. public enum Axis_of_translation
  27. {
  28. x, y, z
  29. }
  30. [HideInInspector]
  31. public Axis_of_translation Comb_Axis_translation;
  32. [HideInInspector]
  33. float Drvr_teeth, Drvn_teeth;
  34. [HideInInspector]
  35. public float distance;
  36. [HideInInspector]
  37. public float manager_total_time;
  38. Camera MainCamera;
  39. void Start()
  40. {
  41. MainCamera = Camera.main;
  42. Drvr_teeth = parentGear.Driver_gear_teeth;
  43. Drvn_teeth = parentGear.Driven_gear_teeth;
  44. Ratio = Drvr_teeth / Drvn_teeth;
  45. print(distance);
  46. if (Comb_Axis_translation == Axis_of_translation.y)
  47. {
  48. Max_Height = Comb.transform.localPosition.y;
  49. if (parentGear.manager.DefaultZ >= parentGear.manager.MinValue &&
  50. parentGear.manager.DefaultZ <= parentGear.manager.MaxValue &&
  51. parentGear.manager.direction == Manager.Axis.z)
  52. {
  53. Default_Y_Location = 2f * Mathf.PI * Radius_of_Gear * ((90 - parentGear.manager.DefaultZ) * Ratio) / 360;
  54. Comb.transform.localPosition = new Vector3(Comb.transform.localPosition.x, Comb.transform.localPosition.y - Default_Y_Location, Comb.transform.localPosition.z);
  55. Comb_Total_Movement = (Max_Height - Comb.transform.localPosition.y );
  56. if (parentGear.manager.DefaultZ == parentGear.manager.MaxValue)
  57. {
  58. Comb_Move_up = false;
  59. Comb_Total_Movement = 2f * Mathf.PI * Radius_of_Gear * (parentGear.manager.MaxValue - parentGear.manager.MinValue) * Ratio / 360;
  60. }
  61. }
  62. else
  63. {
  64. Default_Y_Location = 2f * Mathf.PI * Radius_of_Gear * ((Ratio*(90-parentGear.manager.MinValue)) / 360);
  65. Comb.transform.localPosition = new Vector3(Comb.transform.localPosition.x, Comb.transform.localPosition.y - Default_Y_Location, Comb.transform.localPosition.z);
  66. Comb_Total_Movement = 2f * Mathf.PI * Radius_of_Gear * (parentGear.manager.MaxValue - parentGear.manager.MinValue) * Ratio / 360;
  67. }
  68. }
  69. }
  70. // Update is called once per frame
  71. void Update()
  72. {
  73. manager_total_time = parentGear.manager.total;
  74. ray = new Ray(MainCamera.transform.position, MainCamera.transform.forward);
  75. if (Physics.Raycast(ray, out hit, parentGear.Interaction_Range, layerMask))
  76. {
  77. current = hit.collider.gameObject;
  78. if (Input.GetKeyDown(KeyCode.E) && current == parentGear.Driver_gear)
  79. {
  80. StartCoroutine(LeverMove());
  81. }
  82. }
  83. }
  84. IEnumerator LeverMove()
  85. {
  86. if (Comb_Axis_translation == Axis_of_translation.y)
  87. {
  88. if ( Comb_Move_up == true)
  89. {
  90. while (Comb_Move_up)
  91. {
  92. time += 20f * Time.deltaTime * parentGear.Speed;
  93. if (time >= manager_total_time)
  94. {
  95. manager_total_time = parentGear.manager.MaxValue - parentGear.manager.MinValue;
  96. Comb_Total_Movement = 2f * Mathf.PI * Radius_of_Gear * (parentGear.manager.MaxValue - parentGear.manager.MinValue) * Ratio / 360;
  97. time = 0f;
  98. Comb_Move_up = false;
  99. }
  100. distance = 2 * Mathf.PI * Radius_of_Gear * (20f * Time.deltaTime * parentGear.Speed * Ratio) / 360;
  101. Comb.transform.Translate(0f,distance,0f);
  102. yield return null;
  103. }
  104. Comb.transform.localPosition = new Vector3(Comb.transform.localPosition.x, Max_Height-(2*Mathf.PI*Radius_of_Gear*(90-parentGear.manager.MaxValue)*Ratio/360), Comb.transform.localPosition.z);
  105. }
  106. else if( Comb_Move_up == false)
  107. {
  108. while(!Comb_Move_up)
  109. {
  110. time += 20f * Time.deltaTime * parentGear.Speed;
  111. if (time >= manager_total_time)
  112. {
  113. time = 0f;
  114. Comb_Move_up = true;
  115. }
  116. distance = 2 * Mathf.PI * Radius_of_Gear * (20f * Time.deltaTime * parentGear.Speed * Ratio) / 360;
  117. Comb.transform.Translate(0f, -distance, 0f);
  118. yield return null;
  119. }
  120. Comb.transform.localPosition = new Vector3(Comb.transform.localPosition.x, Max_Height - (2 * Mathf.PI * Radius_of_Gear * (90 - parentGear.manager.MinValue) * Ratio / 360), Comb.transform.localPosition.z);
  121. }
  122. }
  123. }
  124. }