LaserReceiver.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. using VRC.Udon;
  5. using System;
  6. [DisallowMultipleComponent]
  7. [AddComponentMenu("Artyom Scripting System/Laser Receiver Controller")]
  8. public class LaserReceiver : UdonSharpBehaviour
  9. {
  10. public GameObject targetObject; // The object to turn on/off
  11. public int laserLayer = 22; // Layer number to detect the laser
  12. public Animator[] animators; // Array to hold Animators
  13. public string[] parameterNames; // Array to hold corresponding parameter names
  14. public GameObject audioObjectTrue; // GameObject to enable when the state changes to true
  15. public GameObject audioObjectFalse; // GameObject to enable when the state changes to false
  16. public GameObject audioObjectLoop; // GameObject to loop while the state is true
  17. public Collider triggerCollider; // Reference to the trigger collider
  18. public Renderer targetRenderer; // Renderer to change material
  19. public Material materialTrue; // Material to apply when the state is true
  20. public Material materialFalse; // Material to apply when the state is false
  21. public int materialIndex = 0; // Index of the material to change
  22. private int colliderCount = 0; // Counter for colliders with the laser layer
  23. private Collider[] collidersInsideTrigger;
  24. private void Start()
  25. {
  26. // Ensure the target object is initially off
  27. if (targetObject != null)
  28. {
  29. targetObject.SetActive(false);
  30. }
  31. // Ensure the looping audio is initially off
  32. if (audioObjectLoop != null)
  33. {
  34. audioObjectLoop.SetActive(false);
  35. }
  36. // Initial check for colliders inside the trigger
  37. UpdateColliderCount();
  38. }
  39. private void Update()
  40. {
  41. // Continuously check for colliders inside the trigger
  42. UpdateColliderCount();
  43. }
  44. private void UpdateColliderCount()
  45. {
  46. // Define the size and position of the box for the OverlapBox check
  47. Vector3 boxCenter = triggerCollider.bounds.center;
  48. Vector3 boxHalfExtents = triggerCollider.bounds.extents;
  49. // Perform the OverlapBox check
  50. collidersInsideTrigger = Physics.OverlapBox(boxCenter, boxHalfExtents, Quaternion.identity, 1 << laserLayer);
  51. // Update the collider count based on the result
  52. int newColliderCount = 0;
  53. foreach (var col in collidersInsideTrigger)
  54. {
  55. if (col.GetComponent<VRC_Pickup>() == null)
  56. {
  57. newColliderCount++;
  58. }
  59. }
  60. if (newColliderCount != colliderCount)
  61. {
  62. colliderCount = newColliderCount;
  63. Debug.Log("Updated collider count: " + colliderCount);
  64. // Turn on or off the target object based on the collider count
  65. if (colliderCount > 0)
  66. {
  67. if (targetObject != null)
  68. {
  69. targetObject.SetActive(true);
  70. }
  71. UpdateAnims(true);
  72. }
  73. else if (colliderCount == 0)
  74. {
  75. if (targetObject != null)
  76. {
  77. targetObject.SetActive(false);
  78. }
  79. UpdateAnims(false);
  80. }
  81. }
  82. }
  83. private void UpdateAnims(bool state)
  84. {
  85. for (int i = 0; i < animators.Length; i++)
  86. {
  87. if (animators[i] != null)
  88. {
  89. animators[i].SetBool(parameterNames[i], state);
  90. }
  91. }
  92. // Enable or disable GameObjects based on the state
  93. if (state)
  94. {
  95. if (audioObjectFalse != null) audioObjectFalse.SetActive(false);
  96. if (audioObjectTrue != null) audioObjectTrue.SetActive(true);
  97. if (audioObjectLoop != null) audioObjectLoop.SetActive(true); // Enable looping audio
  98. if (targetRenderer != null && materialTrue != null) SetMaterial(materialTrue); // Change material to true
  99. }
  100. else
  101. {
  102. if (audioObjectTrue != null) audioObjectTrue.SetActive(false);
  103. if (audioObjectFalse != null) audioObjectFalse.SetActive(true);
  104. if (audioObjectLoop != null) audioObjectLoop.SetActive(false); // Disable looping audio
  105. if (targetRenderer != null && materialFalse != null) SetMaterial(materialFalse); // Change material to false
  106. }
  107. }
  108. private void SetMaterial(Material material)
  109. {
  110. Material[] materials = targetRenderer.materials;
  111. if (materialIndex >= 0 && materialIndex < materials.Length)
  112. {
  113. materials[materialIndex] = material;
  114. targetRenderer.materials = materials;
  115. }
  116. }
  117. }