[Help x 2] Can I separate my sprites from my collider AND how to turn on collider trigger after 2 collisions?

I have (tried to) coded my game such that

[Game Summary] at the beginning: You have a ‘full heart’ sprite ( = 3 lives)

after missing ball once: ball hits lose collider, you lose one life but the ball rebounds back without stopping the game => your full heart becomes a 66% heart

after missing again: ball rebounds again and you heart becomes 33%

one more miss: ball should go through the collider as the GetComponent<BoxCollider2D>().isTrigger = true; code gets executed and the heart sprite changes again to a 0% heart. Lose Level Should Be Triggered.

Problems 1: Now, my sprites and arrays are all okay and changing as they should, but my heart image is on my lose collider which is below the main camera’s scope.


The Full Heart sprite is below the paddle in the midst of the losecollider. (See above)

Is there a way I can put the sprites elsewhere on the screen while maintaining the position of the lose collider?

Problem 2: While the losecollider does change from isTrigger = false to isTrigger = true by checking the trigger box automatically, it does so after the collision => essentially ball bounces back.

I have an Invoke() coded in so there is a slight delay for the death sound to play.

This is my LoseCollider Script

I have searched the unity docs for some stuff but it either pertains to Unity 5 and above (I use 4.7.2 cause I’m a diligent student :slight_smile: ).

Problem 1:
You can make the heart a separate sprite, and then on your trigger script add a public SpriteRenderer and drag the heart to it. You should be able to modify it that way.

Problem 2:
Since it bounces then changes the trigger flag, set it after the second bounce, then that way when it hits again it should go through.

Edit: Code example for #2
void OnCollisionEnter2D (Collision2D collision){ livesLost++; if (livesLost >= maxLives){ AudioSource.PlayClipAtPoint (death, this.transform.position); Invoke ("ChangeLevel", 3); Brick.breakableCount = 0; } else if(livesLost = maxLives-1){ gameObject.GetComponent<BoxCollider2D>().isTrigger = true; LoseLifeSprite(); } else LoseLifeSprite(); }

1 Like

Privacy & Terms