I'm new to Unity.
I am making a 2D breakout game in Unity.
I want to make a sound effect when the Ball hits the Block and Bar.
The following source code script is attached to Ball.
There is no sound effect.
Corresponding source codeusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundController: MonoBehaviour
{
public AudioClip sound1;
public AudioClip sound2;
private AudioSource audioSource;
// Start is called before the first frame update
void Start ()
{
audioSource = GetComponent ();
}
// Update is called once per frame
void Update ()
{
}
private void OnCollisionEnter2D (Collision2D collision)
{
if (collision.gameObject.tag == "Bar")
{
audioSource.PlayOneShot (sound1);
}
if (collision.gameObject.tag == "Block")
{
audioSource.PlayOneShot (sound2);
}
}
}
-If the content of if of OnCollision is set to Debug.Log, characters will be displayed without any problem = I think that there is no problem with collision detection.
-Moving the Audio Listener from the Main Camera to Ball had no effect.
-If i play the sound in the Project window, it can be played without any problem and the sound will be heard.
-Spatial Blend is also 2D.
The version of Unity is 2019.4.10ver.
-
Answer # 1
Related articles
- c # - i want to play three types of sound effects every 3 seconds to make a loop
- c # - play sound effects using unity is trigger
- c # - applying post processing effects
- c # - about the enemy code rushing in unity's 2d action game
- Simple example of implementing sound effects and music functions in iOS App
- Example analysis of system sound effects and custom sound effects in iOS development
- Explain the simple implementation of sound effects and music playback in iOS development
- Android using SoundPool to play sound effects
- Android uses SoundPool to play sound effects
- pygame game journey method to add icon and bgm sound effects
- Android uses SoundPool to play short sound effects
- Android uses SoundPool to play sound effects
- java - i want to play background music and sound effects with processing
- c # - prefabs made with unity's instantiate are not displayed
- c # - i want to switch multiple sound sources in a scene with unity and make a scene transition
- unity - i want to use audiomixer because the sound effects playing in the barrage game playclipatpoint are broken
- c# - i don't know how to get post effects working in concert with the sound
- c # - sound playback in a place different from the place where the unity hit detection occurred
- c # - no sound in unity games
- c # - player production in unity (shooting game)
- c # - i want to display high score and score in unity
- c # - [unity] format exception: input string was not in a correct format error
- c # - i want sloths to move with the ferris wheel gondola (i want to add inertia)
- c # - moving the unity scene doesn't switch as intended
- c # - unity ugui i want to call the process when an overlapping object is clicked
- c # - please tell me how to use the additional components of unity's volunteer production
- c # - i want to display at the previous scroll position when reopening in the scroll view of unity
- c # - unity2d 2d action suddenly i can't move horizontally
- c # - i want to slowly rotate the model to a fixed angle when the flag is set, and slowly return it to the original angle when t
What's wrong? Is it the recognition that there is no sound even if you execute it?
If you get a collision, what about, for example, the Editor is muted?
There is a [Mute Audio], so please check it.
https://baba-s.hatenablog.com/entry/2017/12/28/205400