How to fix this? My Challange attempt

So’ve been trying the challange for half an hour now, but my nearest possible ressult is the game just throwing audio explosions at me as soon as i hit space :smiley:

heres my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RocketKeyInput : MonoBehaviour
{

    AudioSource myAudioSource;

    Rigidbody rigidBody;

    bool playmusic;


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

    // Update is called once per frame
    void Update()
    {
        ProcessInput();
        if (playmusic == true)
        {
            myAudioSource.Play();

        }

        if (Input.GetKey(KeyCode.Space))
        {
            playmusic = true;

            if (Input.GetKeyDown(KeyCode.Space))
            {
                playmusic = false;
                myAudioSource.Stop();


            }

        }

     void ProcessInput()
    {
       

           


        

        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward);
        }

        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward); // notice the -
            // you can also type (Vector3.back)
        }
    }
    }
}

Hi Mike,

First of all, good job on challenging yourself. :slight_smile:

You asked “[h]ow to fix this”. Fix what? Please elaborate a bit further on your problem. What did you expect to happen? What happened instead?

Is this the fix you’re after?

    // Update is called once per frame
    void Update()
    {
        ProcessInput();
        if (playmusic == true && !myAudioSource.isPlaying)
        {
            myAudioSource.Play();
        }

        if (Input.GetKey(KeyCode.Space))
        {
            playmusic = true;
        }

        if (Input.GetKeyUp(KeyCode.Space))
            {
                playmusic = false;
                myAudioSource.Stop();
            }

A post was split to a new topic: I accidentally deleted my project

Great suggestion!

One thing I noticed, though, is that the audio may not play until the next frame. One way to play it in the same frame is the following. For safety, you may also want to check if myAudioSource.isPlaying when stopping the player.

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

        if (Input.GetKey(KeyCode.Space))
        {
            playmusic = true;
        }

        if (playmusic == true && !myAudioSource.isPlaying)
        {
            myAudioSource.Play();
        }

        if (Input.GetKeyUp(KeyCode.Space))
            {
                playmusic = false;
                if (myAudioSource.isPlaying) {
                    myAudioSource.Stop();
                }
            }
1 Like

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

Privacy & Terms