I'm setting up a breakout style game in 2D using Unity 4.3 and I've set the ball and the vertical walls to have a material (physicsMaterial2D) with zero friction and full (1) bounciness. But when the ball hits the vertical wall with a relatively shallow angle the ball just sticks to the wall and not bounce off of it. At larger angles the ball will bounce off the wall but not for shallow angles. Also if I speed up the ball velocity it seems to work ok too but not when it's slower. Any ideas why this is happening?
Something tells me it may have to do with the mass, drag and angular drag of the ball. Check those values and modify them and see what results you get.
+1 --- tweak those values until it stops doing it. If you can't figure it out then upload some screenshots of your object properties in the inspector and I will try to recreate the issue on my end.
Thanks for the input. Here's the deal. The Rigidbody 2D component has zero's for both the linear drag and angular drag. The mass is set to 1 but experimentation with this number does not seem to have a big impact negatively or positively. I set the velocity of the Rigidbody 2D object in script with values of 2f and 2f for the x and y axis respectively. When I change the x value to 1f and keep the y value to 2f the issue is observed. I've created screen shots of the inspector for my ball object and the material. Also some quick videos showing the issue. http://www.underoathgames.com/issues If there is a problem with the links I created you should be able to watch the videos at http://www.underoathgames.com/issues I use the google chrome browser and was able to watch the vids. (They are HTML5 objects)
I have a similar problem with my game (not a Breakout style game, but a physics-playground sorta game, where you knock a ball around). I'm adding forces to the ball, rather than setting the velocity directly, and it still happens. I suspect there's a stickiness threshold on the walls that isn't exposed in Unity. EDIT: Hah, searching for Box2D sticking to walls turned up pretty much exactly that, right away. http://stackoverflow.com/questions/5381399/how-can-i-prevent-a-ball-from-sticking-to-walls-in-box2d
Interesting read @Togglesworlh. One thing I forgot to mention is that I'm using an Edge Collider 2D component for the walls. Could this be the problem? I'll try using a Box Collider 2D object instead. This is getting very frustrating Update: I tried using a Box Collider 2D component and got the same results. Also I switched the components to use the 3D versions for the colliders and material, and also got the same issue, no bounce. So if the 3D is behaving just like the 2D am I doing something horribly wrong or does this latest release of Unity 4.3 have some major glitches.
The 3D PhysicsManager variables allow modification of the Bounce Threshold. I bet if you change that to 0 (Edit->Project Settings->Physics), it'll always bounce with 3D physics. It's just that they forgot to expose the equivalent variable for the 2D physics, I guess.
Well I changed the Bounce Threshold to zero and used 3D physics but I still got the "sticky" wall syndrome. Not sure what's going on here.
Box2D has a bunch of compile-time constants and therefore are immutable at runtime. We have considered exposing some of these constants at runtime but it obviously involves modifying Box2D itself and therefore is prone to creating issues with dynamically allowing these to change at runtime. We believe it's important to investigate which of these we can safely expose however and so you can expect some of them to be exposed in the future.
MelvMay, I did use the 3D physics colliders, materials and rigidbodys in Unity 4.3 and still got the sticky wall issue. I have been able to achieve what I'm attempting in previous versions of Unity using the 3D physics engine. So I'm a little confused as to why now I'm getting this problem.
funny, i am working on a breakout style game aswell and ran earlier into this issue today. fiddling with the friction fixed it for me.
I had a similar issue in a 3D game, but found a workaround. I didn't know about the bounceThreshold parameter, and it would have fixed it. But if the problems remains for now in 2D, my workaround might still be usefull. So, underoathgames, you can add a script on your wall that store into a list each object that enter in collision with it. When the object leaves, remove them from the list. If the list not empty for X seconds, add a for to each of the rigidbodies in that list, orthogonal to the wall. Just kick'em right in the face, you might say. I'd like to have a better solution though.
@Mogitu, I'm glad fiddling with friction worked for you. I might keep messing with that parameter but I have a felling it won't work. @Berenger, I thought about doing something like that which I might have to implement if I can't get it working any other way. For my style of game it is critical for the ball to bounce off the wall Thanks for the suggestions.
I handled the bouncing myself, using physics only for collision detection, and the result seems to be quite satisfactory. There might be some glitches with several balls though. Code (csharp): using UnityEngine; public class Ball : MonoBehaviour { // Constant speed of the ball private float speed = 5f; // Keep track of the direction in which the ball is moving private Vector2 velocity; // used for velocity calculation private Vector2 lastPos; void Start () { // Random direction rigidbody2D.velocity = Random.insideUnitCircle.normalized * speed; } void FixedUpdate () { // Get pos 2d of the ball. Vector3 pos3D = transform.position; Vector2 pos2D = new Vector2(pos3D.x, pos3D.y); // Velocity calculation. Will be used for the bounce velocity = pos2D - lastPos; lastPos = pos2D; } private void OnCollisionEnter2D(Collision2D col) { // Normal Vector3 N = col.contacts[0].normal; //Direction Vector3 V = velocity.normalized; // Reflection Vector3 R = Vector3.Reflect(V, N).normalized; // Assign normalized reflection with the constant speed rigidbody2D.velocity = new Vector2(R.x, R.y) * speed; } }
Thanks so much Berenger, the code looks great. I didn't realize there was a Reflect function as part of the Vector3 class. I need to do a little reading up on that class. This just might work for me, I'll play around with it, thanks again.
@Berenger Amazing this works perfectly I was having the same issues with the bounce material, thanks for your help
Hi all, I have the very same problem. I'm happy I found this thread! Could I ask to Unity development team if the next release of Unity will allow us to set the Box2D minimum velocity threshold for elastic collisions property? Thank you in advance for your answer, and happy new year!
Don't know if this will help, but under the physics, are two values called sleep velocity and angular sleep velocity. Since you only really have the ball to worry about, try setting these to 0 and see if it works. I'm guessing they are simply hitting the wall, getting a split second of the velocity being to low making it enter into sleep. If you have a lot of physics objects and only want the one object to keep moving no matter what, use this code Code (csharp): if (rigidbody.IsSleeping() ) { rigidbody.WakeUp(); } in the update loop. This will force it to ALWAYS check physics and apply forces and such. Putting this on specific items is probably the best way to do it, as the first mentioned way will make all physics objects always apply forces and such. Doesn't sound like a big thing, but if you have 1000 physics objects in a scene and only 3 are actually moving, thats 997 extra calculations for no reason, so the coded method is best. EDIT: I should also mention, this is what I had to do for some things in 3d. I don't know for certain it will work in 2d objects.
@Caliber Mengsk: Thank you for your answer, but unluckly it seems that the cause of the problem is actually the minimum velocity threshold for elastic collision property of Box2D. Under that speed threshold, the bounce does not occurs, so we need that Unity exposes that value so that we can change it to zero, instead of the default value. @Unity dev team: Will the next release of Unity allow us to change that value? Thank you very much in advance for your answer!
I've just submitted a suggestion to allow to change the value of the b2_velocityThreshold constant of Box2D. Please, vote for it if you have the problem described in the first post of this thread.
Thanks for posting the script Berenger, this is the exact behavior I was looking for. You should get a medal.
For me Berengers script didn't seem to work.. i may have got something wrong! however i found a solution which made it way easier to do this and it uses the physics system built into unity so makes it way easier with less scripting! what you have to do is lower the bounce threshold to 0.1 (apparently it will be all jittery if you change it to 0) it has a default of 2 and this is basically the minimum velocity threshold that something is travelling at and can still bounce! to change it go to Edit >> Project Settings >> Physics and you'll see 'Bounce Threshold' hope this helps
Berenger, post: with bounce code... Thank you for the code! This worked great and easily inserted into my existing script!
thank u for code ..i was going to write my one code for bouncing...!!! link to know about vector reflection http://www.3dkingdoms.com/weekly/weekly.php?a=2
Hi all, I have the very same problem, velocity Threshold (0.01) worked for me Edit-->ProjectSetting-->physics 2D