LaserReceiver.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. [UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
  5. [DisallowMultipleComponent]
  6. [AddComponentMenu("Artyom Scripting System/Laser Receiver Controller")]
  7. public class LaserReceiver : UdonSharpBehaviour
  8. {
  9. public GameObject targetObject; // The object to turn on/off
  10. public int laserLayer = 22; // Layer number to detect the laser
  11. public Animator[] animators; // Array to hold Animators
  12. public string[] parameterNames; // Array to hold corresponding parameter names
  13. public GameObject audioObjectTrue; // GameObject to enable when the state changes to true
  14. public GameObject audioObjectFalse; // GameObject to enable when the state changes to false
  15. public GameObject audioObjectLoop; // GameObject to loop while the state is true
  16. public Collider triggerCollider; // Reference to the trigger collider (this object's collider if empty)
  17. public Renderer targetRenderer; // Renderer to change material
  18. public Material materialTrue; // Material to apply when the state is true
  19. public Material materialFalse; // Material to apply when the state is false
  20. public int materialIndex = 0; // Index of the material to change
  21. [Tooltip("Log each change of the number of lasers inside")]
  22. public bool debugMode;
  23. private int colliderCount = 0; // Counter for colliders with the laser layer
  24. private Collider[] collidersInsideTrigger = new Collider[16]; // Reused every frame
  25. private BoxCollider box; // Set when the trigger is a box: checked exactly, rotation included
  26. private void Start()
  27. {
  28. if (triggerCollider == null) triggerCollider = GetComponent<Collider>();
  29. if (triggerCollider == null)
  30. {
  31. Debug.LogError("[AUMSS] Laser Receiver Controller: no Trigger Collider assigned, and none on this object.");
  32. enabled = false; // Stops Update, which would otherwise throw and halt the script
  33. return;
  34. }
  35. box = triggerCollider.GetComponent<BoxCollider>();
  36. if (box != triggerCollider) box = null; // Another collider on the same object: fall back to the bounds
  37. // Ensure the target object is initially off
  38. if (targetObject != null)
  39. {
  40. targetObject.SetActive(false);
  41. }
  42. // Ensure the looping audio is initially off
  43. if (audioObjectLoop != null)
  44. {
  45. audioObjectLoop.SetActive(false);
  46. }
  47. // Initial check for colliders inside the trigger
  48. UpdateColliderCount();
  49. }
  50. private void Update()
  51. {
  52. if (triggerCollider == null) return; // Not set up (enabled = false only takes effect next frame)
  53. // Continuously check for colliders inside the trigger
  54. UpdateColliderCount();
  55. }
  56. private void UpdateColliderCount()
  57. {
  58. Vector3 boxCenter;
  59. Vector3 boxHalfExtents;
  60. Quaternion boxRotation;
  61. if (box != null)
  62. {
  63. Transform boxTransform = box.transform;
  64. boxCenter = boxTransform.TransformPoint(box.center);
  65. boxHalfExtents = Vector3.Scale(box.size, boxTransform.lossyScale) * 0.5f;
  66. boxHalfExtents = new Vector3(Mathf.Abs(boxHalfExtents.x), Mathf.Abs(boxHalfExtents.y), Mathf.Abs(boxHalfExtents.z));
  67. boxRotation = boxTransform.rotation;
  68. }
  69. else
  70. {
  71. // Other collider shapes: the world-space box around them
  72. boxCenter = triggerCollider.bounds.center;
  73. boxHalfExtents = triggerCollider.bounds.extents;
  74. boxRotation = Quaternion.identity;
  75. }
  76. int found = Physics.OverlapBoxNonAlloc(boxCenter, boxHalfExtents, collidersInsideTrigger, boxRotation,
  77. 1 << laserLayer, QueryTriggerInteraction.UseGlobal);
  78. // Update the collider count based on the result
  79. int newColliderCount = 0;
  80. for (int i = 0; i < found; i++)
  81. {
  82. if (collidersInsideTrigger[i].GetComponent<VRC_Pickup>() == null)
  83. {
  84. newColliderCount++;
  85. }
  86. }
  87. if (newColliderCount != colliderCount)
  88. {
  89. colliderCount = newColliderCount;
  90. if (debugMode) Debug.Log("[AUMSS] Laser Receiver Controller: lasers inside: " + colliderCount);
  91. // Turn on or off the target object based on the collider count
  92. bool on = colliderCount > 0;
  93. if (targetObject != null)
  94. {
  95. targetObject.SetActive(on);
  96. }
  97. UpdateAnims(on);
  98. }
  99. }
  100. private void UpdateAnims(bool state)
  101. {
  102. int count = Mathf.Min(animators.Length, parameterNames.Length);
  103. for (int i = 0; i < count; i++)
  104. {
  105. if (animators[i] != null && !string.IsNullOrEmpty(parameterNames[i]))
  106. {
  107. animators[i].SetBool(parameterNames[i], state);
  108. }
  109. }
  110. // Enable or disable GameObjects based on the state
  111. if (state)
  112. {
  113. if (audioObjectFalse != null) audioObjectFalse.SetActive(false);
  114. if (audioObjectTrue != null) audioObjectTrue.SetActive(true);
  115. if (audioObjectLoop != null) audioObjectLoop.SetActive(true); // Enable looping audio
  116. if (targetRenderer != null && materialTrue != null) SetMaterial(materialTrue); // Change material to true
  117. }
  118. else
  119. {
  120. if (audioObjectTrue != null) audioObjectTrue.SetActive(false);
  121. if (audioObjectFalse != null) audioObjectFalse.SetActive(true);
  122. if (audioObjectLoop != null) audioObjectLoop.SetActive(false); // Disable looping audio
  123. if (targetRenderer != null && materialFalse != null) SetMaterial(materialFalse); // Change material to false
  124. }
  125. }
  126. private void SetMaterial(Material material)
  127. {
  128. // sharedMaterials: .materials would make a new copy of every material on each change
  129. Material[] materials = targetRenderer.sharedMaterials;
  130. if (materialIndex >= 0 && materialIndex < materials.Length)
  131. {
  132. materials[materialIndex] = material;
  133. targetRenderer.sharedMaterials = materials;
  134. }
  135. }
  136. }