Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 latsushi · Oct 09, 2018 at 10:47 PM · audiotriggerplay

Multiple Audio Sources On One Object

I've attached two audio sources to one game object. On one Audio Source I've selected the 'suckedIn' audio file for 'AudioClip' on the Audio Source. For the other Audio Source I've selected the 'pushedOut' audio file for 'AudioClip' on this other Audio Source. The object that the audio sources are attached to essentially behaves like the ball in pong. You can imagine that.

When the ball moves left across the the trigger object, which is called 'Suction' I want the 'suckedIn' audio file to play. When the ball moves right across the trigger object, which again is called 'Suction' I want the 'pushedOut' audio file to play.

The problem I'm having now is that only one audio file plays when it goes left and right through this 'Suction' object. The one audio file that plays is whichever Audio Source is higher in the Inspector that is attached to this ball object (the object with both audio sources attached to it.)

Here's my code:

Notice the two declared Audio Sources and the last two lines in the Start Method. Also, notice the last two if statements in the OnTriggerEnter2D method. This is the relevant information.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Ball : MonoBehaviour {
 
     [SerializeField]
     float speed;
 
     float radius;
     Vector2 direction;
 
     AudioSource suckedIn;
     AudioSource pushedOut;
 
     // Use this for initialization
     void Start () {
         direction = Vector2.one.normalized; // direction is (1, 1) normalized
         radius = transform.localScale.x / 2; // half the width
 
         suckedIn = GetComponent<AudioSource>();
         pushedOut = GetComponent<AudioSource>();
     }
     
     // Update is called once per frame
     void Update () {
         transform.Translate(direction * speed * Time.deltaTime);
 
         // Bounce off top and bottom
         if (transform.position.y < GameManager.bottomLeft.y + radius && direction.y < 0)
         {
             direction.y = -direction.y;
         }
         if (transform.position.y > GameManager.topRight.y - radius && direction.y > 0)
         {
             direction.y = -direction.y;
         }
 
         // Game over
         //if (transform.position.x < GameManager.bottomLeft.x + radius && direction.x < 0)
         //    {
         //    Debug.Log("Right player wins!!");
 
         //    // For now, just freeze time
         //    Time.timeScale = 0;
         //    enabled = false; // Stop updating script
         //    }
         if (transform.position.x > GameManager.topRight.x - radius && direction.x > 0)
         {
             Debug.Log("Left player wins!!");
 
             // For now, just freeze time
             Time.timeScale = 0;
             enabled = false; // Stop updating script
         }
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Paddle")
         {
             bool isRight = other.GetComponent<Paddle>().isRight;
 
             // If hitting right paddle and moving right, flip direction
             if (isRight == true && direction.x > 0 && other.tag == "Paddle") {              
                 direction.x = -direction.x;
             }
             // If hitting left paddle and moving left, flip direction
             if (isRight == false && direction.x < 0)
             {              
                 direction.x = -direction.x;
             }
         }
         if (other.tag == "Wall")
         {
             direction.x = -direction.x;
             Debug.Log("You've hit the wall");
         }
         if (other.tag == "Suction" && direction.x < 0)
         {
             suckedIn.Play();
             Debug.Log(suckedIn);
         }
         if (other.tag == "Suction" && direction.x > 0)
         {
             pushedOut.Play();
             Debug.Log(pushedOut);
         }
     }
 }
 



Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

134 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Play Audio Sound OnTriggerEnter 2 Answers

Stream multiple audio files 1 Answer

Need help on audio trigger. 2 Answers

Problem with streaming audio and play in trigger area 0 Answers

audio will not play if trigger collides 2 Answers


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