- Home /
Weird Looping of Audio.
First of all, let me start by saying I'm a complete noob at coding as of now. I recently made a game its a replica of the 'Flappy Bird' game. Everything worked the way I wanted it to work, no problem's there, all physics, colliders etc. are working perfectly. The problem arose when I was implementing sounds to the game I was able to implement all the sounds which I wanted to be present in the game but when the bird dies the death sound just loops in a weird manner like an electric buzzer for about 1 second and then it goes away. I was trying to figure out what's being wrong in the script but as a beginner, I am unable to figure out what's the mess. Please help me out, folks! I have an exhibition at my college this Saturday and I am thinking to present this game there as a part of my project... So here is the code.
using UnityEngine;
using System.Collections;
public class BirdMovement : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public float flapSpeed = 100f;
public float forwardSpeed = 1f;
float nextSoundTime=0;
bool didFlap = false;
Animator animator;
public bool dead = false;
float deathCooldown;
public bool godMode = false;
void Start () {
animator = transform.GetComponentInChildren<Animator>();
if(animator == null) {
Debug.LogError("Didn't find animator!");
}
}
void Update() {
if(dead) {
SoundManagerScript.PlaySound ("sfx_hit_01");
deathCooldown -= Time.deltaTime;
if(deathCooldown <= 0) {
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
Application.LoadLevel( Application.loadedLevel );
}
}
}
else {
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
SoundManagerScript.PlaySound ("sfx_wing");
didFlap = true;
}
}
}
void FixedUpdate () {
if(dead)
return;
GetComponent<Rigidbody2D>().AddForce( Vector2.right * forwardSpeed );
if(didFlap) {
GetComponent<Rigidbody2D>().AddForce( Vector2.up * flapSpeed );
animator.SetTrigger("DoFlap");
didFlap = false;
}
if(GetComponent<Rigidbody2D>().velocity.y > 0) {
transform.rotation = Quaternion.Euler(0, 0, 0);
}
else {
float angle = Mathf.Lerp (0, -90, (-GetComponent<Rigidbody2D>().velocity.y / 3f) );
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
void OnCollisionEnter2D(Collision2D collision) {
if(godMode)
return;
animator.SetTrigger("Death");
dead = true;
deathCooldown = 0.5f;
}
}