- Home /
PlaySound While Key is pressed
Hello! I would like to implement a basic method like increasing the volume of the mixer while the key is pressed. I've got the script where I can pick up, rotate, throw and release so I add the command to increase the volume and it works but it does it fast and then gets back to -80dB. Sorry for my English
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering.PostProcessing; using UnityEngine.Audio;
[RequireComponent(typeof(MeshRenderer))] public class PickUp02 : Interactable { [SerializeField] private Material higlightMaterial; [SerializeField] private Transform grabHolder; [SerializeField] private float attachSpeed = 1f;
[SerializeField] private float throwForce;
//[SerializeField] private AudioSource audioSource;
[SerializeField] private AudioMixer mainSound;
[SerializeField] private string volume;
[SerializeField] float volumeMultiply;
private Material defaultMaterial;
private MeshRenderer show;
private Rigidbody physics;
private bool isDragged;
void Start()
{
grabHolder = GameObject.Find("holder").transform;
show = GetComponent<MeshRenderer>();
physics = GetComponent<Rigidbody>();
defaultMaterial = show.material;
}
public override void OnHitEnter()
{
base.OnHitEnter();
show.material = higlightMaterial;
}
public override void OnHitExit()
{
show.material = defaultMaterial;
}
public override void OnHitOver()
{
base.OnHitOver();
}
public override void OnHitPress()
{
StartCoroutine(Attach(grabHolder.position));
physics.isKinematic = true;
base.OnHitPress();
}
public override void OnHitPlay()
{
physics.isKinematic = true;
mainSound.SetFloat(volume, 1f * volumeMultiply);
base.OnHitPlay();
}
public override void OnHitRelease()
{
StopAllCoroutines();
physics.isKinematic = false;
base.OnHitRelease();
}
public override void OnHitThrow()
{
StopAllCoroutines();
physics.isKinematic = false;
GetComponent<Rigidbody>().AddForce(grabHolder.forward * throwForce);
base.OnHitThrow();
}
private IEnumerator Attach(Vector3 holderPosition)
{
float t = 0;
while (Vector3.Distance(holderPosition, transform.position) > 0.1f)
{
transform.position = Vector3.Slerp(transform.position, holderPosition, t);
t += Time.deltaTime * attachSpeed;
//focus.weight = t;
yield return null;
}
transform.parent = grabHolder;
transform.localPosition = Vector3.zero;
yield return null;
}
Your answer
Follow this Question
Related Questions
Sound stop when entering trigger 3 Answers
Audio Manager 1 Answer
Play sound on mouse enter 1 Answer
Why is my backtrack's volume getting lower? 0 Answers
Audio attached but still doesn't work 2 Answers