Hello mates! I am new to Unity and am trying to attach sound to the moving platform with the rocket thrust in the 2D GamePlay tutorial. Any help would be much appreciated!
I don’t remember what’s going on in that tutorial, but to play a sound you need an AudioListener on your camera (it’s probably already there) an AudioSource on the object that will emit the sound, and an AudioClip in the clip field of the AudioSource, so there is something to play. Inside the code, when the event that produce the sound occures, audio.Play(). Simple as that.
In order to play a sound from a GameObject, you would need to attach an AudioSource as a component. Click on the moving platform in your scene, then go to:
Component => Audio => Audio Source
to add an Audio Source. From there, you will need to pass in an AudioClip to play from code. If you are simply wanting to have an audio clip always play from the Audio Source, click on the Game Object you just attached the Audio Source to, click on the little bullseye next to Audio Clip, assign the clip of your choice, and then make sure Play On Awake is checked.
Hi, I am still having a problem hooking up this sound to the "Positive Thrust Rocket Jet" which is part of the MovingPlatform. I tried to attach an audio source to the moving platForm game object but the sound is not triggering when the platform moves up.
<p>// We will turn on our special effects when the platform is raising, but if it is moving side
// to side, how fast does it have to be moving to cause our special effects to turn on?
var horizontalSpeedToEnableEmitters = 1.0;</p>
<p>var audioClip : AudioClip;
var volumeModifier = 1.0;</p>
<p>// A true/false (boolean) variable to keep track of whether or not our special effects are
// currently turned on.
private var areEmittersOn : boolean;</p>
<p>// We are going to use these later to calculate the current velocity.
private var oldPosition : Vector3;
private var currentVelocity : Vector3;</p>
<p>function Start() {
// Grabs the initial position of the platform.
oldPosition = transform.position;
}</p>
<p>function Update() {
// Remember if our emitters were on, then we'll see if they are currently on.
wereEmittersOn = areEmittersOn;</p>
<p>// The emitters are on if the vertical (y) velocity is greater than 0 (positive), or if the
// horizontal velocity in either direction (positive or negative speed) is greater than
// our horizontalSpeedToEnableEmitters threshold.
areEmittersOn = (currentVelocity.y > 0) || (Mathf.Abs(currentVelocity.x) > horizontalSpeedToEnableEmitters);</p>
<p>// We only have to update the particle emitters if the state of them has changed.
// This saves needless computation.
if (wereEmittersOn != areEmittersOn) {
// Get every child ParticleEmitter in the moving platform.
for (var emitter in GetComponentsInChildren(ParticleEmitter)) {
//Simply set them to emit or not emit depending on the value of areEmittersOn
emitter.emit = areEmittersOn;
}
}</p>
<p>}</p>
<p>function LateUpdate () {
currentVelocity = transform.position - oldPosition;
oldPosition = transform.position;
}</p>
<p>// This line tells Unity to nicely place this script in a submenu of the Component menu.
@script AddComponentMenu("2D Platformer/Moving Platform/Moving Platform Effects")</p>