- Home /
2 sounds at the same time
Hello Guys. First of all, sorry for my bad English
I want to show you my little script
var MyAudio : AudioClip;
var hasPlayed = false;
function Update (){
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 100.0)) {
if (hasPlayed == false)
{
hasPlayed = true;
audio.clip = MyAudio;
audio.Play();
}
}
else
{
hasPlayed = false;
audio.Stop();
}
}
So, with this script I want to play a sound if I hit an object, and the sound should stop if I release my fingers from the touchscreen. This part works fine.
But now I have a Problem:
If I want to hit two different objects at the same time, the sound is not playing. If I release one finger, the sound is playing again.
I think it's because on a Computer you can have only one mouse,too. And I think the problem is this part:
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
But I dont know, because I'm a beginner.
Thank you for you answers :)
You need to use touch.position where Input.mousePosition is for any kind of touch on mobile devices
look in the Scripting Ref for Input.touches What Exactly are you wanting to do? Do you want continuous sound on touch and then when you lift your finger the sounds stop?
Thank you for your fast answer :)
I've seen the Ref for Input.touches but its still very difficult.
I want to start the sound, if I touch this obejct and when i lift my finger the sound should stop. The sound should not be contiuned, the sound should start every time at the beginning
So I have somehow to add Touchphase.Began and Touchphase.Ended right ?
Another question i should have asked is are you touching a 3d GameObject in the scene or are you touching a 2d GUI type object for the screen to start your sounds?
Answer by Triqy · Nov 10, 2013 at 01:56 PM
this is code to do what your asking. The mySounds var is an AudioClip Array. Just drag and drop your sound in the inspector when you apply this script to an object in your scene. Also you have to set up tags for your objects or the raycast will not see your objects you are touching. Please vote me up if this was helpful. thanks!
this will play two different sounds at the same time when touching two different objects. This will only play once and then you have to touch them again.
#pragma strict
var mySounds: AudioClip[];
var playSound1 = false;
var playSound2 = false;
function Start () {
}
function Update () {
for (var touch : Touch in Input.touches){
var ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, Mathf.Infinity)){
if(hit.collider.gameObject.tag == "YourGameObject.tag"){
playSound1 = true;
if(playSound1 == true){
audio.PlayOneShot(mySounds[0]);
playSound1 = false;
}
}else if(hit.collider.gameObject.tag == "YourGameObject#2.tag#2"){
playSound2 = true;
if(playSound2 == true){
audio.PlayOneShot(mySounds[1]);
playSound2 = false;
}
}
}
}
}
hi I get an error :
NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:376)
SoundObject.Update () (at Assets/Scripts/SoundObject.js:16)
I built a Scene and tested it and built it out to tablet and i forgot to add Require Component. REVISED:
#pragma strict
@script RequireComponent(AudioSource)
@script RequireComponent(AudioListener)
var mySounds: AudioClip[];
var playSound1 = false;
var playSound2 = false;
function Start () {
}
function Update () {
for (var touch : Touch in Input.touches){
var ray : Ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, $$anonymous$$athf.Infinity)){
if(hit.collider.gameObject.tag == "test1"){
playSound1 = true;
if(playSound1 == true){
audio.PlayOneShot(mySounds[0]);
playSound1 = false;
}
}else if(hit.collider.gameObject.tag == "test"){
playSound2 = true;
if(playSound2 == true){
audio.PlayOneShot(mySounds[1]);
playSound2 = false;
}
}
}
}
}
this is suppose to go on the main camera it maywork on an empty gameobject, but didn't test that.
Other than that i didnt get any errors. This script plays the sound over and over until you lift your finger
Your answer
![](https://koobas.hobune.stream/wayback/20220613121538im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Playing a range of sounds from an object 0 Answers
Can not play a disabled audio source persistent problem 0 Answers
loop animation while audio is playing 0 Answers
sound controlled objects? 1 Answer
Cant repeat Sound 1 Answer