Block Breaker randomly ends game with Ball still on Paddle

I have reached this point in the series and when I run the game it randomly ends after some period of time. The ball remains stuck to the paddle and it doesn’t matter whether I move the mouse or not. How can i narrow down why this trigger event is being called?

Platform: Unity 5.5.2f1

I have added in a series of print statements and can confirm that the ball does indeed trigger the OnTriggerEnter2D event with a y axis that would trigger the event.

However, as you can see in the screen shot, the ball is right on the paddle and then jumps to the spot where the trigger is called.

I have checked the script execution order and it is set to Paddle[100], Ball[200]

The lose collier is set to be under the scene.

LoseCollier Script:

public class LoseCollider : MonoBehaviour {

    public LevelManager levelmanager;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        print("Trigger: " + collision.name + " y:"  + collision.transform.position.y);
        levelmanager.LoadLevel("Win");
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        print("Collision");
    }
}

Level Manager

public class LevelManager : MonoBehaviour {

	public void LoadLevel(string name)
	{
		Debug.Log ("Level load requested: " + name);
		SceneManager.LoadScene(name);

    }
	
	public void QuitRequest()
	{
		Debug.Log("Quit");
		Application.Quit();
	}
}

And the ball

public class Ball : MonoBehaviour {

    public Paddle thePaddle;
    private Vector3 paddleToBallVector;

	// Use this for initialization
	void Start () {
        paddleToBallVector = this.transform.position - thePaddle.transform.position;
        print("Vector"+paddleToBallVector.ToString());
	}

    void Update () {
        
        this.transform.position = thePaddle.transform.position + paddleToBallVector;
        print("Ball: "+this.transform.position + " Paddle: " + thePaddle.transform.position);
    }
}
2 Likes

Hello @James_Gyarmathy,

the ball is right on the paddle and then jumps to the spot where the trigger is called.

Do you mean the ball falls through the paddle, enters the trigger collider, and then this triggers the loading of the Win scene?

Hi @Rob

No, the ball never seems to move. I’ve let it sit without even touching the mouse and after about 15 - 20 seconds the LoseCollider OnTriggerEnter2D event is called. The image on the screen never changes; however, in the Console you can see that the Y pos of the Ball Collider Object has changed and triggered the event.

1 Like

If you could zip your project up and upload it, either here, or somewhere else I can get to it, I will have a look for you.

That would be awesome. Thanks Rob.

Block Breaker.zip (7.0 MB)

Having a look now…


Are you sure you are using 5.5.2? I’ve just tried to open it and it is asking me to upgrade it? No biggy, just curious.

Updated Thu Mar 02 2017 13:54

Aha… a project in a project! :slight_smile:

yup, 5.5.2f

Oddly, if i add physics on the ball and leave it running, the game doesn’t end.
FYI: Haven’t finished the video lesson yet, so this might not exactly be the right way to do it :slight_smile:

public class Ball : MonoBehaviour {

public Paddle thePaddle;
private Vector3 paddleToBallVector;
private bool gameStarted = false;

// Use this for initialization
void Start () {
    paddleToBallVector = this.transform.position - thePaddle.transform.position;
    print("Vector"+paddleToBallVector.ToString());
}

void Update () {
    Debug.Log("Update time :" + Time.deltaTime);

    if (!gameStarted)
    {
        this.transform.position = thePaddle.transform.position + paddleToBallVector;
        print("Ball: " + this.transform.position + " Paddle: " + thePaddle.transform.position);
    }
}

private void FixedUpdate()
{
    Debug.Log("FixedUpdate time :" + Time.deltaTime);

    if (Input.GetMouseButtonDown(0))
    {
        print("Left Mouse button clicked");
        gameStarted = true;



        this.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 20f);
    }
}

}

Turning Collision Detection: Continuous on for the Ball’s Rigidbody2D seems to remove the issue. You’d kinda want that on anyway, but this is the first time I’ve seen it triggering a collider when it hasn’t even moved!

I’m still looking but so far that one change has resolved it.

1 Like

Cool, I’ll try that.

I also moved all my code to FixedUpdate and i can actually watch the ball slowely sinking into the paddle and then off the screen…clues. :slight_smile:

private void FixedUpdate()
{
    Debug.Log("FixedUpdate time :" + Time.deltaTime);

    if (!gameStarted)
    {
        this.transform.position = thePaddle.transform.position + paddleToBallVector;
        print("Ball: " + this.transform.position + " Paddle: " + thePaddle.transform.position);
    }

    if (Input.GetMouseButtonDown(0))
    {
        print("Left Mouse button clicked");
        gameStarted = true;
        this.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 20f);
    }
}

That does keep it from ending. I can also now see the ball dribbling on the paddle.

That would perhaps be the gravity kicking in. You shouldn’t need to use FixedUpdate() unless you are applying forces etc, and in the course you just a velocity from memory.

I don’t like the gravity being on to be honest for this type of game (personal preference), the ball kinda “curved” downwards when it got into those horizontal boring loops (you have this to come!)… you can turn it off by changing the Rigidbody2D's Gravity Scale to 0.


Updated Thu Mar 02 2017 14:14

Confirmed about the gravity. If you set the scale to zero as above, run the game and wait for the 20 seconds, nothing happens… then in the inspector, set it back to 1, give it a few seconds and the original issue occurs. Odd though that the ball doesn’t appear to move on the screen.

Heh, without gravity the ball looks like it it slowly launches off the paddle (note i’ve added in the mouse click code)

I have also noticed that if you turn off Simulated on the Ball’s Rigidbody2D it resolves the issue also. This option wasn’t in Unity 4.6.x and as such wouldn’t have been mentioned in the course.


*BOOM*

Untiy Documentation : Indicates whether the rigid body should be simulated or not by the physics system.

Oddly, if you move the below line from Start() to Update() in the if(!gamestarted) statement it resolved the issue.

paddleToBallVector = this.transform.position - thePaddle.transform.position;

Turning simulated off stops the ball from launching off the pad :frowning:

lol - I was just wondering about that… I think that might be because you are using the AddForce method of moving it… that said, I was just going to test whether if I launched the ball this end, whether it would “bounce” off of the paddle… I don’t recall this simulated option in the older version of Unity.


Updated Thu Mar 02 2017 14:30

Ok, so boom--; :frowning:

This really is odd… I’ve popped all the settings back and it has reverted to triggering the collider.

The ball should remain fixed above the paddle, the script execution order will control that also. Putting the Ball before the Paddle script for example will make it kinda wave left and right…

I think it has something to do with this vector math:

paddleToBallVector = this.transform.position - thePaddle.transform.position;

If i move the paddle slowly and launch, it works fine. If I shake the paddle a bunch and then launch, it falls right through the paddle.

It’s weird. The only thing that seems to resolve the issue is to launch the ball. Perhaps i should put a timer on that and let the user know that they lose if they don’t move fast enough :slight_smile:

Gotta run to the job they pay me for.

hmm… it shouldn’t happen!

I will keep looking and post back if I find anything else.


Updated Thu Mar 02 2017 15:34

Ok, I think I have found something else. I opened up another version which I had here, a stock version matching the course like for like, built in Unity 5.4.1. I don’t recall ever having the issue with that version, or, waiting to see if it would happen - so I waited. Nope, doesn’t happen.

I have tried numerous things and found many ways to prevent the problem, but all of these were additional, e.g. I hadn’t need to do these before to prevent this problem.

My theory is that, with the gravity, the physics is calculating where the ball should be, and this is triggering the collision before the sprite is moved on the screen.

Having gone back to your default project, straight out of the .zip file, I have managed to remove the problem by bringing the ball closer to the paddle at the start of the game. Instead of a y position of 0.88, I set it to what I had in a previous project (fairly arbitrarily I should add) which was 0.865. Zooming in within Scene view there is a fraction of a gap, but it really is a fraction.

When I run the game now, there is no problem.

I think what is/was happening was that there was enough of a difference for the physics to calculate a downward movement, this got missed by the collider on the paddle, and whilst still calculating the balls speed moving downwards (increasing because of the gravity), it did get detected by the lose collider. Because all of the physics are calculated before the scene rendering this may explain why you don’t actually see the ball move.

Looking at Unity’s Execution Order of Events and Functions I think this would explain it, it certainly feels somewhat random and definitely not something I have seen before, but that may be entirely luck based on the Y position of the ball!

I am more than happy to be proven wrong and will enjoy reading a better explanation if someone else wants to investigate further :slight_smile:

(still worth setting the Collision Detection to Continuous btw)

2 Likes

This is very interesting and i appreciate all the research.

I went ahead and set the value to 0.865 and indeed the game does not end anymore. The unfortunate side effect is that the ball no longer bounces (which it does at 0.88). The ball ceases to bounce on any number lower than 0.868 / game randomly ends at this number. Ball doesn’t bounce at 0.867 / game doesn’t randomly end.

I added a whole bunch of print statements to try and determine if an actual float value was being stored for the paddletoballvector and it does look like its the case. Printing the vector as an object rounds the values printed, printing discrete values produces the correct value.

So at this point, without some type of deep debugging there does not seem to be a way to complete this game. Could this an actual bug in the software. Is there anything else i can do?

Privacy & Terms