[HELP] Lose Collider triggers on its own after some seconds

After making the ball stick to the paddle when moving (haven’t done the click to launch yet),
I noticed this problem , but I am not sure when it appeared first.
No matter what I do, without even moving the paddle, it just triggers and its game over. I checked the code , its the same.
In the console, the only thing that shows up is “trigger” , followed by “level request for win” some seconds after playing. (the print command we made in a previous lecture)
Any ideas?

Code: LoseCollider::

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

public class LoseCollider : MonoBehaviour {

	public LevelManager levelmanager; 
	void OnTriggerEnter2D (Collider2D trigger) {
		print ("Trigger");
		levelmanager.LoadLevel ("win");
		
	}

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

}

Code: Paddle:

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

public class paddle : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);

		float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;

		paddlePos.x = Mathf.Clamp (mousePosInBlocks, 0.5f, 15.5f); 

		this.transform.position = paddlePos;
	}
}

code: Ball

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

public class Ball : MonoBehaviour {
	public paddle paddle; 

	private Vector3 paddleToBallVector;

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

EDIT: When I untick Is Trigger in Lose Collider, NOTHING happens (by that I mean, the message collision doesnt appear) so the ball doesnt actually touch the collider. What is going on?

Have you checked the shape of the collider on the paddle and the lose area, it sounds like something may be triggering the collision other than the ball(possibly the paddle or a block). I think you may be able to add this to the lose game collision code it will tell you what is causing it to trigger.

Debug.Log(gameObject.name+" has been hit by "+collision.gameObject.name);

Hello! Thanks for replying. I did check that and its correct. I figured out a temporary solution, but I didn’t find the cause, so the thread is still with a [HELP] tag. So, because I had noticed Ben had the paddle and the ball make contact, though mine had a very small space between them, I moved the ball so it touches the paddle. Magically, the problem disappeared. Any ideas?

Have you also checked your paddle collider sits above the lose collider? There should be a gap between the paddle collision area and lose game collision area, ideally somewhere around the same width as your paddle. If it’s too close to the lose area when the ball hits the paddle it will keep going ever so slightly as the game cycles through its runtimes at which point it starts to bounce away but it has also already moved down to trigger the lose game area.

No, thats not it. IT isnt an obvious problem,I think.
The disgtance between them is pretty great. Anyways. Thanks for trying to help.

I Have an issue with my LoseCollider similar to the OP. It appears to be triggering without a reason…

When does this happen?
After you have lost the first ball a new ball appears on the paddle waiting for you to release. If you don’t release but just wait, you will lose your life at a random moment somewhere between 10 and 300 seconds.

What have I done to find the issue?
Log all triggers - On the console all instances where this happens are logged, i.e. a message is logged everytime the LoseCollider is triggered. That’s how I isolated the issue, it is not in the lives calculation or elsewhere. It is caused by ‘firing’ the trigger.
The game can sort of work around the issue by only counting OnTriggerEnter2D when the game has started but the issue still is there:

My Console log:

Ball (Ball)
UnityEngine.Debug:Log(Object)
Paddle:Start() (at Assets/C Scripts/Paddle.cs:18)

Time: 4.92The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = True
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:21)

The player has 2 lives UnityEngine.Debug:Log(Object)
LevelManager:DownTheDrain() (at Assets/C Scripts/LevelManager.cs:38)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:23)

Time: 4.92The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)

Time: 226.7The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)

Time: 230.04The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)

Time: 233.4The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)
Time: 250.22The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)

Time: 253.56The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)

Time: 256.9The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
nityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)

Time: 263.64The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:28)
etc. etc.

Could there be an object out of place triggering the collision?
To see if the paddle or the ball is causing the trigger through some glitch I have logged the location of the paddle and the ball. It appears that they are not entering the Collider2D.

My console Log:

Ball (Ball)
UnityEngine.Debug:Log(Object)
Paddle:Start() (at Assets/C Scripts/Paddle.cs:18)
Time: 4.02The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = True
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:25)
Current Paddle Location = (7.7, 0.2, 0.0)
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:30)
Current Ball Location = (11.0, 0.0, 0.0)
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:34)
The player has 2 lives
UnityEngine.Debug:Log(Object)
LevelManager:DownTheDrain() (at Assets/C Scripts/LevelManager.cs:38)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:41)

(Note for Gamedev forum: the above is correct. I was playing and dropped the ball)

Time: 83.6The OnTriggerEnter in LoseCollider.cs is executed with StartStatus = False
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:25)

Current Paddle Location = (0.7, 0.2, 0.0)
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:30)

Current Ball Location = (3.4, 0.1, 0.0)
UnityEngine.Debug:Log(Object)
LoseCollider:OnTriggerEnter2D(Collider2D) (at Assets/C Scripts/LoseCollider.cs:34)

(Note for Gamedev forum: I didn’t play just waited for the trigger to happen. At the 83 second mark is triggered. Both Paddle and ball were not missplaced or in the area of the LoseCollider)

I can log a lot more of these but the result is the same. It keeps triggering and never because the ball or paddle hits the collider.

edit: It is also not the smoke because this also occurs when the first ball only strikes the white brick and not the breakables. Oh, did I mention it occurs not with the first ball but after the first it keeps happening.

Where exactly does this go wrong?

To provide a bit of additional context I added a screengrab from my Unity view so you can see the location of the ball, paddle and losecollider.
I din’t post more info on the scripts because A) the OP already did that and B) I am fairly sure this is a problem in Unity and not in the scripts.
However, if more info is required I would gladly provide more.

To summarize: how the hell do we stop LoseCollider Box Collider 2D from triggering without provocation?

Anyone?
@Rob ?

Hi @Duckfest_JF ,

You’re paddle is fairly close to that collider, however I would guess that it is the ball which is triggering the collision, especially if you do not have Continuous set for the Collision Detection on the Ball.

The first thing I would consider trying, before you make any changes, would be to ascertain exactly what is creating the collision, to do this you can just add a Debug.Log statement to the OnCollisionEnter2D method in the LoseCollider.

For example;

/// <summary>
/// Handles the collision when the ball goes out of play
/// </summary>
/// <param name="collider">The other Collider2D involved in this collision</param>
private void OnTriggerEnter2D(Collider2D collider)
{
    Debug.Log("The object was : " + collider.gameObject.name);
    // levelManager.LoadLevel("Lose");
}

You could comment out the next line, as I have done above, to prevent the next statement executing, not that it really matters, we just want to see what appears in the console.

Try this and let me know what object it was please :slight_smile:

1 Like

@FoxTheLegend,

So, because I had noticed Ben had the paddle and the ball make contact, though mine had a very small space between them, I moved the ball so it touches the paddle. Magically, the problem disappeared. Any ideas?

You won’t get a message back saying that something has triggered a collision if the colliders of both GameObjects are already within each other. If you have removed the gap and now the ball and paddle are touching, their colliders will be overlapping and thus ignore each other whilst in that state (for that OnTriggerEnter2D/OnCollisionEnter2D messages anyway).


See also;

Thanks, @Rob for your quick response

I feel so stupid :see_no_evil:. I have spent hours eliminating options and preparing my post. My problem solving approach was good, I just didn’t have the patience to sit back and let the data sink in. If I had taken the time to really look at the results of my Debug.Log regarding paddle and ball placement I would have already seen it.

It was around the time of your reply I was just noticing the ball was in all instances too low, lower than expected given the Y-value for the paddle. Still above the 0.1 mark so not yet sure…

After also implementing your suggestion there is no room left for doubt: Even the console blames the ball for the collision! :policeman:

So this means

  • I have to lower the LoseCollider to provide more '‘buffer’'
    or
  • there is another more elegant solution that has the word continuous in it. :thinking:

(anyway, not sure what the cause of the problem is but my guess would be gravity pulling the ball down before the placing it on the paddle occurs? But since this was my first guess I have looked really carefully at both Scene and Game during play, there is no visual/view where the ball hits the collider.)

You’re more than welcome, and your walk through of what you had tried and were looking at was very good.

Regarding the continuous collision detection on Rigidbody2D component , I would recommend that you do apply this. If you consider the objects in your game, which are moving, which are moving fast, and which are not moving…

The ball is going to be traveling the fastest and runs the greatest risk of passing into/through something without registering an event, so for that, continuous collision detection makes sense. It will be bouncing off of all manner of things.

The paddle, not so much, you have it clamped so it doesn’t really need to be detecting anything other than the ball, I would only change the settings on the paddle if you notice any kind of issues after already changing the collision detection on the ball.

And finally you have the blocks, they are not moving at all (not in the default course game anyway), so much like the paddle, these do not warrant any change.

The exceptions to this would be perhaps from extensive playtesting and very, very fast ball speeds. Experiment and see what happens and then make changes as necessary.

Hope this helps :slight_smile:

Since you are online know, I am going for two more bites of your knowledge :wink::nerd_face:

One question is regarding my best approach for tackling the continuous collision:

  • Was it already covered in the course and did I miss it? If thats the case I will gladly go back
  • Will it be covered later in the course? If so I will park the issue and handle it later
  • If neither, I will dive into it myself at a later time

Just one last thing: I think I understand your post on collision detection and how it matters for fast moving objects like a ball. But I can’t comprehend how, in a game where nothing happens, the ball enters the LoseCollider space. The ball is resting on the paddle. A paddle that is not moving. And I, as a developer, am looking at my screen, not touching the keyboard, not touching the mouse. Waiting for a ball that is still in the same place (as shown in the Scene view and also in the Game view) and at the same time is occasionally in collision with a LoseCollider that is roughly 2 ball sizes away. Can you explain that? Or point me in the right direction to find out for myself?

Thanks so much!

1 Like

Hi Joris,

One question is regarding my best approach for tackling the continuous collision:

  • Was it already covered in the course and did I miss it? If thats the case I will gladly go back
  • Will it be covered later in the course? If so I will park the issue and handle it later
  • If neither, I will dive into it myself at a later time

I honestly can’t remember, I think Ben does touch on it briefly, but more along the lines of “if you are having problems, try this” as opposed to something specific on that item. It has come up numerous times on the forum and with this change the issue is typically resolved.

But I can’t comprehend how, in a game where nothing happens, the ball enters the LoseCollider space.
Can you explain that? Or point me in the right direction to find out for myself?

I would suggest that what is happening is that the physics calculations are happening in the background and more than once per frame. At some point the physics calculations determine that the ball has fallen far enough, due to gravity, to make contact with the lose collider, yet on screen the frame has not yet been updated.

I’ve provided a link below which has a great diagram of the cycle and indicates that the physics may occur more frequently than once per frame.

Some things you could try…

  • Turn off gravity on the ball, run the game, wait… is a collision detected?
  • Is the ball’s collider already making contact with the paddle’s collider? Try raising the ball up a little bit… is a collision detected?

The above are just little tests you could experiment with to see if either make any difference.


See also;

1 Like

Privacy & Terms