using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.Interaction.Toolkit.Interactables;
public class SoundcollisionTEST : MonoBehaviour
{
public AudioSource[] soundEffects;
public bool isSoundPlaying;
public Transform soundPos;
public float impactForce;
private BoxCollider boxCollider;
private bool hasCollided;
private MeshRenderer meshRenderer;
private XRGrabInteractable grabInteractable;
void Start()
{
soundPos = GetComponent();
boxCollider = GetComponent();
meshRenderer = GetComponent();
grabInteractable = GetComponent();
isSoundPlaying = false;
}
void Update()
{
// On collision, lock the soundPos once then reset flag
if (hasCollided)
{
soundPos.transform.position = soundPos.transform.position;
hasCollided = false;
Debug.Log(soundPos.transform.position);
}
}
private void OnCollisionEnter(Collision collision)
{
// Check for impact force threshold
if (collision.relativeVelocity.magnitude >= impactForce)
{
// Prevent playing if already active
if (soundEffects[0].isPlaying || soundEffects[1].isPlaying)
return;
// Play both sounds
soundEffects[0].Play();
soundEffects[1].Play();
hasCollided = true;
isSoundPlaying = true;
// Disable visuals and interactivity
meshRenderer.enabled = false;
grabInteractable.enabled = false;
boxCollider.enabled = false;
}
}
}