LinkedObjectSystem.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. [DisallowMultipleComponent]
  5. [AddComponentMenu("Artyom Scripting System/Linked Object System")]
  6. public class LinkedObjectSystem : UdonSharpBehaviour
  7. {
  8. [Header("Script Settings\n")]
  9. [Tooltip("Print all variables to console for debug purposes")]
  10. public bool debugMode;
  11. [Tooltip("Delay for Temporary interact, ignore if not needed")]
  12. public float delayedInteractTime = 0f;
  13. public LayerMask excludeLayers;
  14. [Header("Interactions Settings\n")]
  15. [Tooltip("Trigger behavior type")]
  16. public TriggerType triggerType = TriggerType.None;
  17. [Tooltip("Interact via select type")]
  18. public InteractionType interactionType = InteractionType.None;
  19. [Header("Animator Links (bool only)\n")]
  20. public Animator[] outputAnimators;
  21. public string[] outputParameterNames;
  22. [Space]
  23. public GameObject audioObjectTrue;
  24. public GameObject audioObjectFalse;
  25. // PRIVATE VARS
  26. private bool playerTriggerEnabled = false;
  27. private bool pickupTriggerEnabled = false;
  28. [UdonSynced]
  29. private int isOccupied;
  30. private float timer;
  31. void Start()
  32. {
  33. isOccupied = 0;
  34. if (triggerType == TriggerType.BothPlayerAndObject)
  35. {
  36. playerTriggerEnabled = true;
  37. pickupTriggerEnabled = true;
  38. }
  39. else if (triggerType == TriggerType.Player)
  40. {
  41. playerTriggerEnabled = true;
  42. pickupTriggerEnabled = false;
  43. }
  44. else if (triggerType == TriggerType.Object)
  45. {
  46. playerTriggerEnabled = false;
  47. pickupTriggerEnabled = true;
  48. }
  49. if (interactionType == InteractionType.None)
  50. {
  51. DisableInteractive = true;
  52. }
  53. }
  54. private void Update()
  55. {
  56. if (interactionType == InteractionType.Temporary && isOccupied == 1)
  57. {
  58. if (debugMode) Debug.Log("Linked Object System: Interaction Type Checked As Temprary");
  59. if (timer < delayedInteractTime)
  60. {
  61. isOccupied = 1;
  62. if (debugMode) Debug.LogWarning("LOS: Logged isOccupied as " + isOccupied.ToString());
  63. timer += Time.deltaTime;
  64. if (debugMode) Debug.Log("Linked Object System: DeltaTime Update, New Value -> " + timer.ToString());
  65. }
  66. else if (timer >= delayedInteractTime)
  67. {
  68. isOccupied = 0;
  69. if (debugMode) Debug.LogWarning("LOS: Logged isOccupied as " + isOccupied.ToString());
  70. UpdateAnims(false);
  71. timer = 0f;
  72. }
  73. }
  74. }
  75. private void UpdateAnims(bool state)
  76. {
  77. int count = Mathf.Min(outputAnimators.Length, outputParameterNames.Length);
  78. for (int i = 0; i < count; i++)
  79. {
  80. if (outputAnimators[i] != null && !string.IsNullOrEmpty(outputParameterNames[i]))
  81. outputAnimators[i].SetBool(outputParameterNames[i], state);
  82. }
  83. // Enable or disable GameObjects based on the state
  84. if (audioObjectFalse != null && audioObjectTrue != null)
  85. {
  86. if (!state)
  87. {
  88. if (debugMode) Debug.Log("Linked Object System: GameObject set audio to false");
  89. audioObjectFalse.SetActive(true);
  90. audioObjectTrue.SetActive(false);
  91. }
  92. else
  93. {
  94. if (debugMode) Debug.Log("Linked Object System: GameObject set audio to true");
  95. audioObjectTrue.SetActive(true);
  96. audioObjectFalse.SetActive(false);
  97. }
  98. }
  99. }
  100. private void OnTriggerEnter(Collider other)
  101. {
  102. if (pickupTriggerEnabled && other.GetComponent<VRC_Pickup>())
  103. {
  104. isOccupied++;
  105. if (debugMode) Debug.Log("Linked Object System: Pickup set trigger to " + isOccupied.ToString());
  106. if (isOccupied > 0 && isOccupied <=2)
  107. {
  108. UpdateAnims(true);
  109. }
  110. }
  111. }
  112. public override void OnPlayerTriggerEnter(VRCPlayerApi player)
  113. {
  114. if (playerTriggerEnabled)
  115. {
  116. isOccupied++;
  117. if (debugMode) Debug.Log("Linked Object System: Player set trigger to " + isOccupied.ToString());
  118. if (isOccupied > 0 && isOccupied <=2)
  119. {
  120. UpdateAnims(true);
  121. }
  122. }
  123. }
  124. private void OnTriggerExit(Collider other)
  125. {
  126. if (pickupTriggerEnabled)
  127. {
  128. VRC_Pickup pickup = other.GetComponent<VRC_Pickup>();
  129. if (pickup != null)
  130. {
  131. isOccupied--;
  132. if (debugMode) Debug.Log("Linked Object System: Pickup set trigger to " + isOccupied.ToString());
  133. if (isOccupied == 0)
  134. {
  135. UpdateAnims(false);
  136. }
  137. }
  138. }
  139. }
  140. public override void OnPlayerTriggerExit(VRCPlayerApi player)
  141. {
  142. if (playerTriggerEnabled)
  143. {
  144. isOccupied--;
  145. if (debugMode) Debug.Log("Linked Object System: Player set trigger to " + isOccupied.ToString());
  146. if (isOccupied == 0)
  147. {
  148. UpdateAnims(false);
  149. }
  150. }
  151. }
  152. public override void Interact()
  153. {
  154. if (interactionType.Equals(InteractionType.Permanent))
  155. {
  156. isOccupied++;
  157. if (isOccupied == 1)
  158. {
  159. UpdateAnims(true);
  160. }
  161. }
  162. if (interactionType.Equals(InteractionType.Temporary))
  163. {
  164. if (isOccupied == 0)
  165. {
  166. isOccupied = 1;
  167. if (debugMode) Debug.LogWarning("LOS: Logged isOccupied as " + isOccupied.ToString());
  168. UpdateAnims(true);
  169. DisableInteractive = true;
  170. SendCustomEventDelayedSeconds(nameof(DisableTemp), delayedInteractTime+0.2f);
  171. }
  172. }
  173. if (interactionType.Equals(InteractionType.Toggle))
  174. {
  175. if (isOccupied == 1) {
  176. isOccupied = 0;
  177. UpdateAnims(false);
  178. }
  179. else {
  180. isOccupied = 1;
  181. UpdateAnims(true);
  182. }
  183. }
  184. }
  185. public void DisableTemp()
  186. {
  187. DisableInteractive = false;
  188. isOccupied = 0;
  189. if (debugMode) Debug.LogWarning("LOS: Logged isOccupied as " + isOccupied.ToString());
  190. }
  191. }
  192. public enum InteractionType
  193. {
  194. None,
  195. Permanent,
  196. Temporary,
  197. Toggle
  198. }
  199. public enum TriggerType
  200. {
  201. None,
  202. Player,
  203. Object,
  204. BothPlayerAndObject
  205. }