Alright so i have noticed these little tiny clicking sound whenever my audio play or stop, it’s not much and I can get away with it but still I wanna know how to fix it, make my audio smoother perhaps?
Below is my code, Player code:
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("Movement Section")]
[SerializeField] float moveAmount = 10f;
[SerializeField] float rotationAmount = 20f;
[Header("Status Section")]
[SerializeField] bool isDead = false;
[SerializeField] bool isSuccess = false;
[Header("Particles Effects Section")]
[SerializeField] ParticleSystem mainEnginePE;
[SerializeField] ParticleSystem leftSidePE;
[SerializeField] ParticleSystem rightSidePE;
[SerializeField] ParticleSystem explosionPE;
[SerializeField] ParticleSystem successPE;
Rigidbody rocketRigidbody;
AudioManager gameAudio;
LevelManager levelManager;
private void Awake()
{
gameAudio = FindObjectOfType<AudioManager>();
levelManager = FindObjectOfType<LevelManager>();
}
// Start is called before the first frame update
void Start()
{
isDead = false;
rocketRigidbody = GetComponent<Rigidbody>();
gameAudio.StopEngineAudio();
}
// Update is called once per frame
void FixedUpdate()
{
if (!isDead)
{
ProcessThrust();
ProcessRotation();
}
else if (isDead)
{
gameAudio.StopEngineAudio();
mainEnginePE.Play();
leftSidePE.Play();
rightSidePE.Play();
}
CheatKeys();
}
void ProcessThrust()
{
// Pressing Keys
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
StartThrusting();
}
// if not pressing key or isDead
else
{
StopThrusting();
}
}
private void StopThrusting()
{
// nếu k ấn W và đang có âm thanh Engine thì tắt tiếng engine
gameAudio.StopEngineAudio();
// Dừng PE lại khi nhả phím
mainEnginePE.Stop();
}
private void StartThrusting()
{
// di chuyển
rocketRigidbody.AddRelativeForce(Vector3.up * moveAmount, ForceMode.Force);
// bật Particle Effects
if (!mainEnginePE.isPlaying)
{
mainEnginePE.Play();
}
// khi ấn W và k có âm thanh engine thì sẽ bật tiếng
gameAudio.PlayEngineAudio();
}
void ProcessRotation()
{
RotateRight();
RotateLeft();
}
private void RotateLeft()
{
// Rotate on the left
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
rocketRigidbody.AddTorque(Vector3.forward * -rotationAmount, ForceMode.Force);
if (!rightSidePE.isPlaying)
{
rightSidePE.Play();
}
}
else
//if (!Input.GetKey(KeyCode.A) || !Input.GetKey(KeyCode.LeftArrow))
{
rightSidePE.Stop();
}
}
private void RotateRight()
{
// Rotate on the right
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
// Xử lý quay phải
rocketRigidbody.AddTorque(Vector3.forward * rotationAmount, ForceMode.Force);
// Bật Particle Effects bên trái (đẩy sang phải) nếu PE đang k bật
if (!leftSidePE.isPlaying)
{
leftSidePE.Play();
}
}
else
//if (!Input.GetKey(KeyCode.D) || !Input.GetKey(KeyCode.RightArrow))
{
leftSidePE.Stop();
}
}
void CheatKeys()
{
// if press L, auto win level
if (Input.GetKey(KeyCode.L))
{
levelManager.StartNextLevel();
gameAudio.PlaySuccessAudio();
ActivateSuccessPE();
}
// if press P, disable collisions
else if (Input.GetKey(KeyCode.P))
{
}
}
public void ActivateSuccessPE()
{
successPE.Play();
}
public void ActivateExplosionPE()
{
explosionPE.Play();
}
// Getter Setter
public bool GetIsDeadStatus()
{
return isDead;
}
public void SetIsDeadStatus(bool isDeadValue)
{
isDead = isDeadValue;
}
public bool GetIsSuccessStatus()
{
return isSuccess;
}
public void SetIsSuccess(bool isSuccessValue)
{
isSuccess = isSuccessValue;
}
}
and here is my Audio Code
using UnityEngine;
public class AudioManager : MonoBehaviour
{
[Header("Engine Sound Effects")]
[SerializeField] AudioClip engineThrustSFX;
[SerializeField][Range(0, 1)] float engineVolume = 1f;
[Header("Landing Pad Success SFX")]
[SerializeField] AudioClip successSFX;
[SerializeField][Range(0, 1)] float successVolume = 1f;
[Header("Explosion SFX")]
[SerializeField] AudioClip explosionSFX;
[SerializeField][Range(0, 1)] float explosionVolume = 1f;
Transform rocketTransform;
AudioSource gameAudioSource;
[Header("Locations")]
[SerializeField] GameObject landingPad;
private bool isSounding;
void Awake()
{
rocketTransform = FindObjectOfType<Player>().GetComponent<Transform>();
gameAudioSource = GetComponent<AudioSource>();
}
void Start()
{
}
void Update()
{
UpdateAudioPosition();
}
// Get trạng thái biến bool isSounding
public bool GetSounding()
{
return isSounding;
}
// hàm dùng chung để bật âm thanh tuy nhiên khá là tù vì làm ntn thì k ngắt được âm thanh một khi đã bậts
private void PlayAudioClip(AudioClip clip, Vector3 audioLocation, float volume)
{
// không thể huỷ PlayClipAtPoint một khi đã chạy
AudioSource.PlayClipAtPoint(clip, audioLocation, volume);
}
// ngắt âm thanh động cơ tên lửa
public void StopEngineAudio()
{
if (gameAudioSource.isPlaying)
{
gameAudioSource.Stop();
}
}
// chạy âm thanh tên lửa
public void PlayEngineAudio()
{
if (!gameAudioSource.isPlaying)
{
//PlayAudioClip(engineThrustSFX, engineVolume);
gameAudioSource.PlayOneShot(engineThrustSFX, engineVolume);
}
}
// update vị trí của âm thanh theo vị trí của tên lửa
void UpdateAudioPosition()
{
gameAudioSource.transform.position = rocketTransform.position;
}
// hàm chạy âm thanh success
public void PlaySuccessAudio()
{
PlayAudioClip(successSFX, landingPad.transform.position, successVolume);
}
public void PlayExplosionAudio()
{
PlayAudioClip(explosionSFX, rocketTransform.position, explosionVolume);
}
}
i have done everything in my knowledge to stop it but well, to no good till now, send helpp