Something going on with my audio. (Unity6 - Rocket Boost game)

I’ve been loving this Unity black friday deal course. I’m building the cartoony rocket game right now and adding SFX (re: this lesson). This audio plays without any keyboard input after I start the scene and I think it’s trying to play over itself every frame, sounds like a stuttering buzzing. I’m trying to move on but it bugs me, because I thought I had it right. Here’s my AudioSource inspector:
AudioSourceInspector
and my script. I really appreciate any eyeballs you can share. Thanks!

using UnityEngine;
using UnityEngine.InputSystem;

public class Movement : MonoBehaviour
{
    [SerializeField] InputAction thrust;
    [SerializeField] InputAction rotation;
    [SerializeField] float thrustStrength = 100f;
    [SerializeField] float rotationStrength = 50f;
    Rigidbody rb;
    AudioSource audioSource;
    
    private void OnEnable()
    {
        thrust.Enable();
        rotation.Enable();
    }

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }

    private void FixedUpdate()
    {
        ProcessThrust();
        ProcessRotation();
    }

    private void ProcessThrust()
    {
        if (thrust.IsPressed())
            rb.AddRelativeForce(Vector3.up * thrustStrength * Time.fixedDeltaTime);
            if (!audioSource.isPlaying)
            {
               audioSource.Play();
            }
        else 
        {
            audioSource.Stop();
        }
    }

    private void ProcessRotation()
    {
        float rotationInput = rotation.ReadValue<float>();
        //Debug.Log("Rotation Value is: "+ rotationInput);
        if (rotationInput < 0)
        {
            ApplyRotation(rotationStrength);
        }
        else if (rotationInput > 0)
        {
            ApplyRotation(-rotationStrength);
        }
    }

    private void ApplyRotation(float rotationThisFrame)
    {
        rb.freezeRotation = true;
        transform.Rotate(Vector3.forward * rotationThisFrame * Time.fixedDeltaTime);
        rb.freezeRotation = false;
    }

    private void Update()
    {

    }
}

BTW, this is adorable:

Hi Gangrel512,

Your sceen looks nice. Well done so far. :slight_smile:

Regarding the problem with the audio, if code blocks that are wrapped in an if-block get executed unexpectedly, there are usually three reasons: a) the condition gets evaluated to true even though you expect it to be false, b) the condition is wrong, c) the curly brackets are misplaced.

In C#, curly brackets define a code block which consists of more than one line of code.

If you cannot spot any problem in your code, have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?


See also:

1 Like

I figured it out (thank goodness) , I missed the braces around this IF statement:

     if (thrust.IsPressed())
            rb.AddRelativeForce(Vector3.up * thrustStrength * Time.fixedDeltaTime);
            if (!audioSource.isPlaying)
            {
               audioSource.Play();
            }
1 Like

Good job! :slight_smile:

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

Privacy & Terms