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 :slight_smile:

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?

1 Answer

1

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;
		}

}
}
}
}

var ray doesn't have a type definition.

i dont get any errors

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)

Other than that i didnt get any errors. This script plays the sound over and over until you lift your finger

humm does't work for me... Okay I place the script on a Plane..the plane has a Plane (Mesh Filter) Mesh Collider Mesh Renderer And the srpict.. I enter in size 1 and place the sound in Element 0 & check Play Sound 1.. but the same 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)