- Home /
How to use OnCollisionEnter() and OnTriggerEnter() with physics to make ball bounce off and pass through bricks?
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!!!
thank you fro introducing me to Ignore Collsion function.....however now when i call Physics.IgnoreCollision() it causes the ball collider to ignore all colliders?
Basically when I call Physic.IgnoreCollision() inside of FixedUpdate() or inside Update() then the ball colliders ignores all colliders. If i call Physic.IgnoreCollision() inside of OnCollisionEnter() then it will only ignore the collision until the second time it hits the brick?
Thanks Once again:)
Your answer
Follow this Question
Related Questions
OnTriggerEnter function in c# 0 Answers
Disable Meshrenderer OnTriggerEnter 0 Answers
NavMeshAgent and OnTriggerEnter/OnTriggerExit 0 Answers
OnTriggerEnter works once, OnTriggerExit not at all? 1 Answer
How to Apply a script During collison? 3 Answers