- Home /
Audio play problem on raycast hit colider.gameObject.tag
This is the part of my script.
if(hit.collider.gameObject.tag == "Enamy")
{
lineRenderer.SetColors(color3, color4);
if (!hit.collider.audio.isPlaying) hit.collider.audio.Play();
}
Basicaly I want raycast to hit the gameobject in the scene and play sound when it hits colider tag "Enamy"
the AudioSource is atached to the gameobject.
I want it to be continous sound but I have a problem...
When I move the camera away and raycast is not hiting gameobject that is tagged anymore sound keeps playing ,so how do I play the audio only when ray is hiting gameobject that is tagged?
Ok I managed to fix the problem by editing the script this way so it stops when I am not raycasting the tagged object.
if(hit.collider.gameObject.tag == "Enamy" )
{
lineRenderer.SetColors(color3, color4);
var ss: SoundScript = hit.transform.GetComponent(SoundScript);
if (ss){
audio.PlayOneShot(ss.soundFX);
}
}
and now ins$$anonymous$$d having an audio source on the tagged object there is a script that is called when object is raycasted.
var soundFX: AudioClip;
and the audio sound I am using is 2d but it plays too fast kinda like there are multiple sounds in the same time and it high pitched and it stops all other sounds that are in the scene like ambient sound.
Your answer
Follow this Question
Related Questions
Audio help. 1 Answer
Detect face of cube clicked 4 Answers
How to deactivate something when holding down a key? 1 Answer
Passing a string from Javascript to C# 1 Answer
how to get the name of an object that a script is attached to 1 Answer