So I’m actually a little surprised that I can’t find much information on something like this. So maybe I’m looking in the wrong direction.
I’m prototyping a top-down soccer-type game and I’m having some trouble working with the physics of Unity2D. Basically, I have a ball and want it to bounce around walls/players but also come to a stop as it looses speed. I figure this could also be applied to other types of games such as beat 'em ups and Zelda-esque adventure games that may use physics so I’d like to get something like this working as nicely as possible.
The way I have it set up now, is that when a player tagged object is moving and collides with the ball tagged object, the ball gets a force applied to it that will move opposite to where the two objects collided. If it bumps into other objects it will also bounce back. This works somewhat but i run into issues trying to get the ball to slow down to a stop and having the ball go off in weird directions if the player continuously collides with the ball. I’m also pretty sure I have to give it a maximum velocity so it doesn’t careen into the ether. Physics 2D Materials help with the bounce, but these seem to be focused on platformer-type views.
I’m currently using Playmaker but can code when I need to (this might be a need to situation) but I’m also not opposed to looking into Unity Asset Store addons if anyone knows of something that can help.
I would a RigidBody2D for colliding and moving the ball. That has setting for both mass ,drag and angular drag. If the ball is shooting too far you can increase its mass… Also drag which default to 0 can be set to cause the ball to slow down on its own. Though for a top down you will probably need to turn gravity 90 degrees. I think Physics.gravity = new Vector3 (0,0,1) should do the trick (so gravity is going into the screen). The bigger the number the stronger the gravity. Also if gravity is causing problems for your player objects, you can stick a rigidBody 2D on them and disable gravity for just them.
Thank you! The Linear Drag and Angular Drag options have made the ball start to slow down on it’s own over time. I must have been over complicating things. I thought that wouldn’t have worked since there’s technically no “floor” beneath the ball.
My only problem now (at the moment at least) is stopping the ball from erratically flying around after two players bounce it back and forth. Right now I have it that if a player is static, the ball slows down by half of its current velocity through Add Force. It works, but if a moving player bounces the ball to a static player while they’re close to each other, the ball goes flying (which is weird, because of the halving speed thing).
I’m not really sure whats happening there. It could be some artifact of bouncing back and forth super fast between 2 players from your Added force being cumulative. I have to admit I’m not well-versed in how Unity Physics work. I do know its all updated during void FixedUpdate. You could have your ball script have a FixedUpdate and add a lot of Debug.Log calls to see the values of the balls transform and physics forces on it. You can access a txt file log of Debug.Log from the Console window. The right side has a little drop down with open editor log
So it turns out the force was being added faster than the drag could reduce the speed. The ball and player must’ve been so close that the bounce was happening faster than I could see how many times it was actually registering the collisions. I added a script that forces the velocity of the X and Y to max out at a certain range and it no longer flies away.