Reputation: 43
I'm having issues playing Audio Clips in Unity.
I want my Shark to produce a "bite" sound when the player is detected but the sound is distorted.
The rest of the code is running as intended.
Can I have a code review and suggestion please?
What am I possibly doing wrong (when calling the Audio Source to Play)?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shark2Controller : MonoBehaviour {
public Transform leftPoint;
public Transform rightPoint;
public float startSpeed;
public float swimSpeed;
private Rigidbody2D myRigidBody;
public bool swimmingRight;
public float superSpeed;
public Transform puntoA;
public Transform puntoB;
public LayerMask whatIsPlayer;
public bool playerDetected;
private Animator myAnim;
public AudioSource bite;
// Use this for initialization
void Start ()
{
myRigidBody = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator>();
swimSpeed = startSpeed;
}
// Update is called once per frame
void Update ()
{
playerDetected = Physics2D.OverlapArea (puntoA.position, puntoB.position, whatIsPlayer);
myAnim.SetBool ("Player Detected", playerDetected);
if (playerDetected)
{
swimSpeed = superSpeed;
bite.Play ();
}
if (swimmingRight && transform.position.x > rightPoint.position.x)
{
swimmingRight = false;
}
if (!swimmingRight && transform.position.x < leftPoint.position.x)
{
swimmingRight = true;
}
if (swimmingRight)
{
myRigidBody.velocity = new Vector3 (swimSpeed, myRigidBody.velocity.y, 0f);
transform.localScale = new Vector3 (1f, 1f, 1f);
}
else
{
myRigidBody.velocity = new Vector3 (-swimSpeed, myRigidBody.velocity.y, 0f);
transform.localScale = new Vector3 (-1f, 1f, 1f);
}
}
public void ResetShark2Speed()
{
swimSpeed = startSpeed;
}
}
Upvotes: 3
Views: 1459
Reputation: 15881
One problem I see is that you're re-playing the sound at every screen update (based on your app's framerate). It's not clear if you want it to loop (since is placed inside a void Update
function for repeated instructions) or you simply want it to play once per detection.
If Unity can detect when a sound is playing or has finished then use that to help fix this.
The looping logic is:
bite.Play();
.A pseudo-code example:
if (playerDetected == true)
{
swimSpeed = superSpeed;
if( bite.isPlaying == true)
{
//# Do nothing or whatever you need
}
else
{
//# Asssume it's not playing (eg: stopped, ended, not started, etc)
bite.Play ();
}
}
For once-per-detection:
public bool detect_snd_Played;
detect_Snd_Played = false; //# Not yet played since no "detect"
//# Update is called once per frame
void Update ()
{
if (playerDetected == true) { swimSpeed = superSpeed; }
if (detect_Snd_Played == false)
{
bite.Play ();
detect_Snd_Played = true; //# Stops future playback until you reset to false
}
}
This line detect_Snd_Played = true;
should stop multiple playbacks until you reset. This could be if your player becomes "un-detected" by shark, and you can now reset for next time's new "detection" process...
Upvotes: 2