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?
– TriqyThank 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 ?
– SumaschAnother 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?
– Triqy