Quantcast
Channel: Questions in topic: "power up"
Viewing all articles
Browse latest Browse all 49

How to use OnCollisionEnter() and OnTriggerEnter() with physics to make ball bounce off and pass through bricks?

$
0
0
Objective:Only make the ball bounce of the brick when the star power up is ***NOT*** activated. If the ball's star power up ***IS*** activated,make the ball travel through the brick without physics affecting the ball. Strategy: If the ball has the star power up activated; then change the bricks colliders into trigger colliers so that physics don't affect the ball bouncing on the bricks. Actual:when the star power up is activated; what's happening, is that the ball will bounce off the brick on the first time its collides with that brick; then on the second time it collides with that brick it will finally pass through the brick without bouncing off. Limitations:I cant set the bricks to always be trigger colliders because i require the bricks to have physics properties when colliding with other game objects. ////////////////////////////////////////////// // THE CODE ///////////////////////////////////////////// //NOTES:The following coding is in the Brick.cs ------- All bricks use this script //NOTES:The brick object knows what a ball is //NOTES: The ball uses capsule collider that has a physics material that makes the ball always bounce in opposite direction void Update() { // change brick physics depending on ball powerup if (this.ball.starActivated == false) this.collider.isTrigger = false; } void OnCollisionEnter(Collision other) { if (this.gameObject.CompareTag("brickCollision") && other.gameObject.tag == "ballCollision")) { // only kill(destroy) the brick if the star powerup is activated if(this.ball.starPowerUpActivated == false) this.killBrick(); // change brick physics depending on ball powerups //if the star powerup is activated then make the brick collider trigger if (this.ball.starPowerUpActivated == true) this.collider.isTrigger = true; } } void OnTriggerExit(Collider other) { if (this.gameObject.CompareTag("brickCollision") && other.gameObject.tag == "sonicStarCollision")) { // only kill(destroy) the brick if the star powerup is activated if(this.ball.starPowerUpActivated) this.killBrick(); } } I tried changing the code from OnTriggerExit() to OnTriggerEnter() and OnTriggerStay(); however, they all have the same result. I suppose i can make the bricks have 2 colliders(1 normal collider and another trigger collider); however i dont want to use so many colliders because there is already too many physics and graphics in the program and i am trying to use the least amount of computer and graphics processing as possible. Someone please help!!!

Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>