- Home /
How to punch a ball without loosing its momentum?
Hi,
I'm trying to code a Breakout/Arkanoid clone with Unitys built-in physics. At the moment I'm working on the behavior of the paddle, when it gets hit by the ball. I would like to have it bounce towards the ball, so the ball bounces back again. So far I assigned the Bouncy material to the paddle and the walls. I also found a cool effect in iTween:
var bounce : int;
function FixedUpdate () {
if (Input.GetButtonDown("Fire1")) {
iTween.PunchPosition(gameObject, iTween.Hash("z",bounce, "time",0.25));
}
}
The animation is cool, but the ball looses height and momentum and therefore stands still after a while. Is there a way to achieve this effect with maybe 'AddForce', so the ball won't loose its "bouncing"-energy?
Answer by MidgardSerpent · Jan 23, 2012 at 11:11 PM
There are a few things that will be slowing your ball down. The first and most obvious one is gravity, which will add a constant downward force to the ball, causing it to slow down and eventually fall off the screen or settle. It's easy to turn off...
The second is drag, which is a setting on the Rigidbody. Drag will slow down the motion of your ball, which will cause that loss of height and momentum over time. In a breakout type game the ball doesn't normally have drag so this should be set to 0.
Simply moving the paddle forward while it's set kinematic might not give you the kind of motion you want, since the momentum of the kinematic paddle isn't going to carry into the collision like it would in real life. A good way to add some extra punch to a collision is get the velocity of the other object after the collision (in OnColliderExit), normalize it... and then multiply it by the magnitude of force you'd like to add... then add it as an impulse to the ball. This will give speed up the ball and give it a good amount of extra force without changing it's direction.
thanks for your answer! There are some tips I didn't know before! I tried everything you wrote and I got to the point, where the ball stays at the same height, yay! Here's my code - let me know, if that's, what you meant or not:
var bounce : int;
var magnitude : int;
private var otherVelocity : Vector3;
function OnCollisionEnter (other:Collision) {
if (other.gameObject.name == "Ball") {
iTween.PunchPosition(gameObject, iTween.Hash("z",bounce, "time",0.25));
}
}
function OnCollisionExit (other:Collision) {
if (other.gameObject.name == "Ball") {
otherVelocity = other.rigidbody.velocity.normalized;
other.rigidbody.AddForce(otherVelocity * magnitude * Time.deltaTime, Force$$anonymous$$ode.Impulse);
}
}
I haven't worked with iTween before... but it looks like it's responsible for how you're doing your punch animation....
Otherwise this looks right, if your ball is still not giving you the kind of behavior you expect from a breakout game, it probably has more to do with the configuration of the ball and the environment than your script.
Answer by Stefan 4 · Jan 26, 2012 at 03:09 PM
yes you're right - it's more about tweaking the physics configurations here and there.
again thanks for your advice!
You're quite welcome.... if it was helpful, please thumbs up the answer!
Dan
Answer by Stefan 4 · Jan 26, 2012 at 03:09 PM
yes, you're right. it's more about tweaking the physics configurations her and there. I'm starting to get the hang of it.
again, thanks for your advice!
Your answer
Follow this Question
Related Questions
Bounce collision vs. constrained rigidbody 2 Answers
breakout ball physics 1 Answer
How do I use rigidbody's collision detection modes? 2 Answers
Breakout-style ball getting stuck horizontally... 3 Answers
Keep constant velocity 4 Answers