- Home /
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);
}
}
}
Your answer
Follow this Question
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