Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sumasch · Nov 09, 2013 at 08:00 PM · objectsoundplay

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Triqy · Nov 09, 2013 at 08:12 PM 0
Share

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?

avatar image Sumasch · Nov 10, 2013 at 12:56 PM 0
Share

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 ?

avatar image Triqy · Nov 10, 2013 at 01:45 PM 0
Share

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 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
 
 }
 }
 }
 }
Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tw1st3d · Nov 10, 2013 at 01:57 PM 0
Share

var ray doesn't have a type definition.

avatar image Triqy · Nov 10, 2013 at 01:58 PM 0
Share

i dont get any errors

avatar image Sumasch · Nov 10, 2013 at 02:26 PM 0
Share

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)
avatar image Triqy · Nov 10, 2013 at 02:53 PM 0
Share

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.

avatar image Triqy · Nov 10, 2013 at 02:53 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges