Untitled
unknown
csharp
a year ago
6.3 kB
16
Indexable
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class SequenceTrigger : UdonSharpBehaviour
{
public GameObject targetObject;
public UdonBehaviour udonBehaviour; // Reference to UdonBehaviour for triggering custom event
public string customInputString; // Public string for custom keyboard input
private string typedString = ""; // Internal string for what the user types
private bool isButtonHeld = false;
private float holdTime = 0f;
private bool canStartSequence = false;
private int sequenceIndex = 0;
private float holdThreshold = 5f; // 5 seconds hold time
// NEW: Animator and bool parameters
public Animator animator; // Reference to the Animator component
public string boolParameterName = "playAnim"; // The name of the bool parameter in the Animator
public float resetTime = 2.0f; // Time after which the bool will be reset to false
private bool isPlaying = false; // Prevents re-triggering during playback
private void Start()
{
targetObject.SetActive(false); // Ensure the object starts off
}
private void Update()
{
HandleButtonHold();
HandleKeyboardInput();
// Only allow sequence if hold was complete
if (canStartSequence)
{
CheckInputSequence();
}
}
private void HandleButtonHold()
{
// Check if the button is being held for 5 seconds (for example, left grip button)
if (Input.GetAxisRaw("Oculus_CrossPlatform_PrimaryHandTrigger") > 0.9f) // Left grip as example
{
if (!isButtonHeld)
{
holdTime = 0f;
isButtonHeld = true;
}
else
{
holdTime += Time.deltaTime;
if (holdTime >= holdThreshold)
{
canStartSequence = true;
Debug.Log("Hold complete, ready for sequence.");
}
}
}
else
{
isButtonHeld = false;
holdTime = 0f;
}
}
private void CheckInputSequence()
{
switch (sequenceIndex)
{
case 0:
if (Input.GetAxisRaw("Oculus_CrossPlatform_SecondaryHandTrigger") > 0.9f) // Right trigger
{
sequenceIndex++;
Debug.Log("Right Trigger Pressed");
}
break;
case 1:
if (Input.GetAxisRaw("Oculus_CrossPlatform_PrimaryHandTrigger") > 0.9f) // Left trigger again
{
sequenceIndex++;
Debug.Log("Left Trigger Pressed Again");
}
break;
case 2:
if (Input.GetAxisRaw("Oculus_CrossPlatform_PrimaryThumbstickVertical") > 0.5f) // Left joystick up
{
sequenceIndex++;
Debug.Log("Left Joystick Up");
}
break;
case 3:
if (Input.GetAxisRaw("Oculus_CrossPlatform_SecondaryThumbstickVertical") > 0.5f) // Right joystick up
{
sequenceIndex++;
Debug.Log("Right Joystick Up");
}
break;
case 4:
if (Input.GetAxisRaw("Oculus_CrossPlatform_PrimaryThumbstickVertical") < -0.5f && Input.GetAxisRaw("Oculus_CrossPlatform_SecondaryThumbstickVertical") < -0.5f) // Both joysticks down
{
sequenceIndex++;
Debug.Log("Both Joysticks Down");
}
break;
}
// If the entire sequence is completed, trigger the object and the custom event
if (sequenceIndex == 5)
{
ActivateObjectAndTriggerEvent();
}
}
private void HandleKeyboardInput()
{
foreach (char c in Input.inputString)
{
// Append characters to the typed string
typedString += c;
// Check if the typed string matches the customInputString
if (typedString.Contains(customInputString))
{
Debug.Log("Custom Input String detected: " + customInputString);
ActivateObjectAndTriggerEvent();
typedString = ""; // Reset the typed string after successful match
}
}
}
private void ActivateObjectAndTriggerEvent()
{
Debug.Log("Sequence Complete or Custom Input Detected! Activating object and triggering custom event.");
targetObject.SetActive(true);
// Trigger the Udon Custom Event called "balls"
if (udonBehaviour != null)
{
udonBehaviour.SendCustomEvent("balls");
}
// Trigger the animation by setting the bool on the Animator (networked)
TriggerNetworkedAnimation();
canStartSequence = false;
sequenceIndex = 0;
}
// Function to trigger the networked animation
private void TriggerNetworkedAnimation()
{
// Ensure the local player is the owner of this object
Networking.SetOwner(Networking.LocalPlayer, gameObject);
// Trigger the networked event to sync the animation across all clients
SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, "StartAnimation");
}
// Networked function to start the animation
public void StartAnimation()
{
if (!isPlaying && animator != null)
{
isPlaying = true;
animator.SetBool(boolParameterName, true); // Set the playAnim bool to true
Debug.Log("Animation started on all clients.");
// Start the reset process to revert the bool back to false after the animation duration
SendCustomEventDelayedSeconds("ResetAnimation", resetTime);
}
}
// Networked function to reset the animation
public void ResetAnimation()
{
if (animator != null)
{
animator.SetBool(boolParameterName, false); // Reset the playAnim bool to false
Debug.Log("Animation reset on all clients.");
isPlaying = false;
}
}
}
Editor is loading...
Leave a Comment