Sound is played before the ball launched

I understand that the if should be set in order to produce sound after mouse button clicked. But I am confused about the value in boolean hasStarted in different stages.

The lecture said that if the hasStarted equals to true, then play the sound. Which means the sound would play once the method LaunchOnMouseClick is run and the mouse left button is clicked, so the value of hadStarted becomes true.

but hadStarted we set equals to false initailly. Therefore, in the method OnCollisionEnter2D, how could if (hasStarted) equals if (hasStarted == true)?

Sorry that I may explain the situation badly. But I am really curious about this. Thanks

Hi,

From memory hasStarted is set to false in its declaration at the top of the script (a bool will always default to false anyway). Then, in the launch method the bool is set to true,because the game has now started. If its value is checked after this point it will equal true until reset.

Probably best to copy/paste your code into a reply (and apply code fencing characters) so we can see what you have.


See also;

Thanks for the reply in advance.
As I went back to listen to the lecture setting the bool ‘hasStarted’ in launch method, I am also confused about the teacher said (I upload a screenshot(1st) below). !hasStarted is not false (it should be true) because we set hasStarted as false initially and there is no code dealing with it so far. hasStarted actually changes to true after the mouse left button clicked to launch the ball.

So back to the sound play. if I didn’t click the mouse left button to launch the ball, hasStarted should be false. If I don’t want to play the sound before I click to launch the ball, the if statement should be (hasStarted == true). The teacher also mentioned in the lecture, but thought hasStarted is true initially. (I also upload three screenshots below.)

Hope my explanation is clear enough :joy:
Thanks again!

public class Ball : MonoBehaviour {
    //config parameters
    [SerializeField] Paddle paddle1;
    [SerializeField] float xPush = 2f, yPush = 10f;
    // state
    Vector2 paddleToBallVector;
    bool hasStarted = false;

	// Use this for initialization
	void Start () {
        paddleToBallVector = transform.position-paddle1.transform.position;
	}
	
	// Update is called once per frame
	void Update () {

        if (hasStarted == false){
            LockBallToPaddle();
            LaunchOnMouseClick();
        }

    }
    private void LockBallToPaddle(){

        Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
        transform.position = paddlePos + paddleToBallVector;

    }

    private void LaunchOnMouseClick(){

        if (Input.GetMouseButtonDown(0))
        {
            hasStarted = true;
            GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);

        }

    }

    private void OnCollisionEnter2D(Collision2D collision){
       
        if (hasStarted == true){
            GetComponent<AudioSource>().Play();

        }
    }
}

Screenshot about ball launching



Screenshots about sound play

Hi,

I’m sorry, I’m a bit lost with regards to your post and what you are saying, the volley of screenshots hasn’t really helped.

!hasStarted and hasStarted == false are the same thing, the ! effectively means “not”.

The reason this is in the Update method is because you only want to call LockBallToPaddle and LaunchOnMouseClick until the mouse is clicked, after that you don’t need to.

The if statement in the OnCollisionEnter2D method shouldn’t be necessary if your Ball isn’t actually touching the Paddle at the beginning, if you leave a small gap so that the circle and box colliders don’t touch, the OnCollisionEnter2D method isn’t called. Your LockBallToPaddle code will then hold it in that place, with the gap.

Once the ball is released by pressing the mouse button then any collision with the ball should trigger the sound.

So, to clarify, are you still experiencing the problem of the sound playing before you launch the ball?

Hello,
After I disable the play on awake, the sound doesn’t work when I press the play button. But I didn’t change my code.

Even though the problem is fixed, I don’t think !hasStarted and hasStarted == false are the same things. When the hasStarted is set initially, hasStarted = false as default. So how could !hasStarted be false? There is no code doing anything to hasStarted before !hasStarted.

Play on Awake means exactly that, as soon as the GameObject is awake, play. If this is disabled, it won’t play when its awake and instead will rely on your telling it, via code, to play.

Consider the following;

if(hasStarted)
{
    // ...
}

this says “If hasStarted = true”

Now consider this;

if(!hasStarted)
{
    // ...
}

The ! character indicates not, and therefore what you are saying is “If hasStarted is not true”.

!hasStarted means we’re expecting the opposite of true, which is false, therefore it is the same as writing;

if(hasStarted == false)
{
    // ...
}

I think you may be getting confused between the value of the boolean, and the evaluation of the statement.

hasStarted can be false, but the evaluation of the statement if(!hasStarted) can be true. e.g. “hasStarted equals false, so run this code”.

!hasStarted could evaluate to being false if hasStarted = false, because not false would mean true - and thus, not false.


See also;

I followed the instructions of the course but when I ran the game I still heard the “click” and I thought I adjusted the get component in the “Update” script. So I did right, I thank you!


Hi,

I’m glad you fixed the issue. :slight_smile:
Is “Play On Awake” enabled or disabled in your AudioSource component?

it is enabled, sir! what problems with it?

If you enabled it, the sound will start playing when the game object is created. That’s the reason why you are hearing a click sound. Disable it., then you can remove the Stop method from Update().

Wow i understand. Thank you so much :smiley:

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

Privacy & Terms