Hello everyone,
I… have a problem…
THE GAME
I’m a new unity programmer learning about all the features and methods in unity. I’m currently building a ball and paddle game in which a ball bounces off the walls at the angle that you launch it. Because I am new I am having a lot of problems and although I can solve most of them with tutorials, experimentation and the unity docs, here is one problem that I just can’t figure out.
Disclaimer: Go ahead and leave this thread if you don’t have time to explain a possibly simple concept. I don’t want to waste your time but I am having a lot of trouble.
THE PROBLEM
I have successfully made the game to where the ball will bounce off the walls and change the direction correctly. My issue though, is that as I move the rigidbody2D of the ball collider2D sinks into the walls instead of stopping immediately after touching a wall collider. This can be solved by switching the collision detection of the ball rigidbody2D to continuous but this doesn’t cause the ball to stop after touching an edge but instead move along the edge. This is a problem because I need to take a reading of the ball position immediately after the ball touches the edge.
How do I stop the ball from sinking into the edge colliders?
MY SOLUTION (failed)
My idea to solve this problem was to as soon as I received OnCollisionEnter I would back the ball out of the wall instantly by using a while loop that stops once the ball isn’t touching the edge collider anymore. This began the hunt of finding a property that was able to detect if the ball collider was touching another collider. The only method I found was IsTouching(); but I wasn’t able to get it working because of my failure to understand the docs.
Definitely let me know either how to use IsTouching(); or another method or property that returns the number of colliders it’s touching.
IDEA #2 (also failed)
With the first idea failing, my next idea was to make a boolean that switched to true when OnCollisionEnter was called and switched to false when OnCollisionExit was called. In OnCollisionEnter using the same while loop I would repeat until the boolean was false.
void OnCollisionEnter2D(Collision2D collision)
{
touchingCollider = true;
for (int i = 0; i < 100 || !touchingCollider; i++)
{
ballRigidbody.MovePosition(ballRigidbody.position + velocity * Time.fixedDeltaTime / 5);
}
}
void OnCollisionExit2D(Collision2D collision)
{
touchingCollider = false;
}
(I used a for loop just so that if something when wrong there was a “breaker” that killed the code after too long)
There’s no surprise this didn’t work because while(and for) loops happen in one frame leaving no time for OnCollisionExit to trigger.
HELP
Thanks so much for taking the time to read my problem. My question ultimately is how do I stop collider2Ds from sinking into each other. (Code examples would be highly appreciated)
Final Disclaimer(feel free to skip): If you tell me that it’s clear in the docs then this is the reason that they only help me some of the time. I am unfamiliar with a lot of the terms that they use, and if I try to learn those terms I will have to learn more terms to learn those and it sets me on a rabbit trail ultimately getting nothing done. Also the fact that they have few code examples makes it nearly impossible for me to visualize what they are saying. I think the docs are useful, but this is the reason that I have trouble with them.