jimmy tremblay-Bernier c1bf5a4ca1 initial commit
2022-03-12 22:04:30 -04:00

109 lines
2.4 KiB
C#

using System.Collections;
using UnityEngine;
using Valve.VR;
public class Horn : MonoBehaviour
{
public Transform trans;
public PlayerManagement pm;
private bool HornOnMouth = false;
private float startTime;
private AudioSource hornFX;
private float startVolume;
public SteamVR_Action_Vibration hapticAction;
private void Start()
{
hornFX = GetComponent<AudioSource>();
startVolume = hornFX.volume;
}
// Set the horn back to it's position
public void Release()
{
transform.position = trans.position;
transform.rotation = trans.rotation;
Rigidbody rbHandler = GetComponent<Rigidbody>();
rbHandler.velocity = Vector3.zero;
rbHandler.angularVelocity = Vector3.zero;
}
public void CountBlowingTime()
{
if (HornOnMouth)
{
hornFX.volume = startVolume;
startTime = Time.time;
hornFX.Play();
Pulse(5, 160, 1, SteamVR_Input_Sources.RightHand);
}
}
public void BlowingHorn()
{
if (HornOnMouth)
{
float timeSpentBlowing = Time.time - startTime;
if ((pm.BraveryGauge >= pm.BraveryGaugeLimit) && timeSpentBlowing >= 4.0f)
{
pm.RingTheHorn();
}
// Fade horn sound out
else
{
StartCoroutine(FadeOut());
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PlayerHead")
{
HornOnMouth = true;
CountBlowingTime();
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "PlayerHead")
{
BlowingHorn();
HornOnMouth = false;
}
// Fade horn if playing
if (hornFX.isPlaying)
{
StartCoroutine(FadeOut());
}
}
private IEnumerator FadeOut()
{
while (hornFX.volume > 0)
{
hornFX.volume -= startVolume * Time.deltaTime / 0.5f;
yield return null;
}
Pulse(1, 0, 0, SteamVR_Input_Sources.RightHand);
hornFX.Stop();
hornFX.volume = startVolume;
}
private void Pulse(float duration, float frequency, float amplitude, SteamVR_Input_Sources source)
{
hapticAction.Execute(0, duration, frequency, amplitude, source);
}
}