Shooting glitch

Everything went fine until I out audioPlayer.PlayShootingClip(); in Shooter.cs. I tested everything else, and I found out that adding that one line of code makes the shooting glitch out

Could someone help me? This is my Audio.cs code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class AudioPlayer : MonoBehaviour

{

[Header("Shooting")]

[SerializeField] AudioClip shootingClip;

[SerializeField] [Range(0f, 1f)] float shootingVolume = 1f;

 [Header("Damage")]

[SerializeField] AudioClip damageClip;

[SerializeField] [Range(0f, 1f)] float damageVolume = 1f;

public void PlayShootingClip()

{

    PlayClip(shootingClip, shootingVolume);

}



public void PlayDamageClip()

{

    PlayClip(damageClip, damageVolume);

}

void PlayClip(AudioClip clip, float volume)

{

    if(clip != null)

    {

        Vector3 cameraPos = Camera.main.transform.position;

        AudioSource.PlayClipAtPoint(clip, cameraPos, volume);

    }

}

}

Hi,

Welcome to our community! :slight_smile:

I’m not sure if I understood your problem correcty. Is the audio the problem or the shooting? If the latter, it would be great if you could also share the code for shooting. Your AudioPlayer class looks fine to me and does not seem to affect the shooting mechanism.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?


See also:

The shooting is the problem. I don’t hear the audio either. Here is my shooting script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Shooter : MonoBehaviour

{

//instantiates projectiles when the OnFire key's pressed

[Header("General")]

[SerializeField] GameObject projectilePrefab;

[SerializeField] float projectileSpeed = 10f;

[SerializeField] float projectileLifetime = 5f;

[SerializeField] float baseFiringRate = 0.3f;

[Header("AI")]

[SerializeField] bool useAI;

[SerializeField] float firingRateVariance = 0f;

[SerializeField] float minFiringRate = 0.1f;



[HideInInspector]

public bool isFiring;

Coroutine firingCoroutine;

AudioPlayer audioPlayer;

void Awake(){

audioPlayer = GetComponent<AudioPlayer>();

}

void Start()

{

    if(useAI){

        isFiring = true;

    }

}

void Update()

{

    Fire();

}

void Fire()

{

    if(isFiring && firingCoroutine == null){

        firingCoroutine = StartCoroutine(FireContinuously());

    } else if (!isFiring && firingCoroutine != null){

        StopCoroutine(firingCoroutine);

        firingCoroutine = null;

    }

}

IEnumerator FireContinuously()

{

    while(true){

        //instantiating the laser in front of the player ship with no rotation

        GameObject instance = Instantiate(projectilePrefab,

                                        transform.position,

                                        Quaternion.identity);

        //applying velocity

        Rigidbody2D rb = instance.GetComponent<Rigidbody2D>();

        if(rb != null){

            rb.velocity = transform.up * projectileSpeed;

        }

        //destroys gameObject after the projectile lifetime has expired

        Destroy(instance, projectileLifetime);

        float timeToNextProjectile = Random.Range(baseFiringRate - firingRateVariance,

                                        baseFiringRate + firingRateVariance);

        timeToNextProjectile = Mathf.Clamp(timeToNextProjectile, minFiringRate, float.MaxValue);

        yield return new WaitForSeconds(timeToNextProjectile);                            

    }

}

}

When I add audioPlayer.PlayShootingClip();, the game shoots like the picture I showed in the last post. Did I explain that correctly?

I assume you did. The problem you described is very odd, though. That’s not your fault. Sometimes, problems are weird.

Have you already tried to remove audioPlayer.PlayShootingClip(); from your code to see if the problem remains? It might be that it is just a coincidence, and the problem might be something else.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe timeToNextProjectile is too close to 0 or negative. If it is, also check the values of the other variables in this line:

float timeToNextProjectile = Random.Range(baseFiringRate - firingRateVariance,
                                        baseFiringRate + firingRateVariance);

If the problem is not caused by timeToNextProjectile, add a Debug.Log at the top of your while-loop in the FireContinuously method. How many times does the coroutine get called when you shoot with your keyboard?

I’m wondering if the problem is caused by this condition:

else if (!isFiring && firingCoroutine != null){

firingCoroutine becomes null if the coroutine stops. The value of isFiring is set to true in Start if useAI is true or in your Inspector.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms