My rocket with Audio

I haven’t watched Ben’s logic yet but this is what I came up with for the Audio to start and stop.

Rigidbody rigidBody;
    AudioSource audioSource;
    bool isRocketAudioKeyDown = false;

    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        ProcessInput();
        RocketAudio();
    }

    void ProcessInput()
    {
        if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
        {
            print("Thrusting!!!");
            rigidBody.AddRelativeForce(Vector3.up);
        }
        if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
        {
            print("Rotating left");
            transform.Rotate(Vector3.forward);
        }
        else if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
        {
            print("Rotating right");
            transform.Rotate(-Vector3.forward);
        }
    }

    void RocketAudio()
    {
        if (Input.GetKey(KeyCode.Space) && !isRocketAudioKeyDown)
        {
            audioSource.Play();
            isRocketAudioKeyDown = true;
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            audioSource.Pause();
            isRocketAudioKeyDown = false;
        }
    }
1 Like

Privacy & Terms