i am somehow able to turn on / off collisions of an object but i have a problem now that i cannot solve.
i have 2 objects - a wall and a ball for example. usually the ball bounces on the wall. when i change something on the ball (for example the color) i want it to go through the wall until i change the color again.
what i tried to do is to turn off the collision when certrain events happen (for example again the color is the one i want to have) in the OnCollisionEnter method and turn it back on when OnCollisionExit happens. the thing is… when i only use OnCollisionEnter it works - the collision is removed and the object passes through. if i also turn on the collision again in oncollisionexit the collision is turned off and on instant and the ball bounces off again (before even getting through the wall).
i wonder why this is happening and how i can prevent it from happening.
What is happening here is that the physics system has already registered the collision by the time OnCollisionEnter is called. If you then disable collisions between the two objects the the collision is immediately exited. However, this re-enables collision detection and the physics procedes with handling the collision. You may be able to get around this problem by converting the wall to a trigger temporarily and then using OnTriggerExit to detect when the ball has passed through.
basically i think it works now but i am running into small problems. i paste my code and describe them… maybe its an obvious mistake.
void OnCollisionEnter(Collision collision) {
<things happening to detect if ball passes through or not>
collision.collider.isTrigger = true;
}
void OnTriggerExit(Collider other) {
other.isTrigger = false;
}
i did rotate the whole thing 90 degrees so the wall is now the floor and i am able to let the ball bounce with a click. also there is a 2nd wall above the ball. sometimes the ball passes through and if i change the setting while he is in the air he lands on the 2nd wall as it should be.
sometimes thou the ball does not pass through and i don’t see a pattern why it’s working sometimes and why it’s not working on other times.
on the ball to get the feeling for it right since the standard gravity is too low and everything seems to be under water. can these things mess up with the collision and trigger stuff?
another problem turned up on this matter: i am using a character controler now. in the
function OnControllerColliderHit (hit : ControllerColliderHit )
function i can find out if the criteria for not colliding is met. if the criteria for passing through is met i am setting the isTrigger to true. so far so good. but how can i find out if my object passed through so i can set the trigger to false again? does the character controler has such a function?