- Home /
Rebound losing energy
Hi, I've been trying to fix my problem for quite some time and I still can't so I hope someone here will be able to. I've go a falling Rigidbody2D (without velocity at start), with y positive :
void Start()
{
rigidbody2d = transform.GetComponent<Rigidbody2D>();
}
When reaching y = 0, I want it to "rebound" (in thin air), for that, I only invert the velocity.y :
private void Update()
{
if (rigidbody2d.position.y < 0f && rigidbody2d.velocity.y < 0f)
{
rigidbody2d.velocity = -Vector2.up*rigidbody2d.velocity.y;
}
}
My object shouldn't lose energy at all because I've set all drags to 0. However, rebound after rebound, the objet goes less and less high. I'd like it to be able to bounce at the same high every time. Obviously, FixedUpdate gives the same result.
If anyone knows what's causing that effect and correct it, please tell me! Thanks in advance
Answer by Eudaimonium · May 11, 2020 at 02:02 PM
Hmm. Replace the velocity invert with:
Vector3 velocity = rigidbody.velocity;
velocity.y *= -1;
rigidbody.velocity = velocity;
What you're doing is only setting a velocity to the new Vector that will have the magnitude equal to previous velocity Y component, instead of being equal to previous velocity magnitude. If the velocity was not perfectly vertical, it will be less after your invert because you discarded X and Z components.
Disclaimer, I have not tested this. Lemme know if it works.
Thanks for your answer! I've tested, it gives the same result I had, each rebound getting lower. In my case, only the Y velocity isn't void as the movement is only afected by gravity and starts still
What exactly is the desired behavior you wish to achieve? Perhaps using physics engine for something like that is a wrong approach. Perhaps try looking into Animator, animation clips, or AnimationCurve class to achieve the desired bouncing effect (of course, if the thing bouncing isn't supposed to react to physics)?
The goal is something more complicated than that, but that example is the easiest one which revealed my problem. In terms of energy, the kinetic energy is void at the beginning, while the gravitational potential energy is at his maximum. When falling, the gravitational potential energy decreases, but the kinetic one rises (total energy remains constant). That way, when I the Y velocity is inverted, the kinetic energy should be able to get the object back to its initial position. $$anonymous$$y problem is that isn't the case and I can't understand why and I need to
Your answer
![](https://koobas.hobune.stream/wayback/20220612233922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Bounce gains energy each time 1 Answer
My flipped character ragdoll has wrong hingejoint anchor positions 0 Answers
Rigidbody2D.IsKinematic giving object reference not set? 0 Answers
Drag to add power, release to throw at opposite angle (RigidBody2D), strange beahviour. 3 Answers
2DColliders going through eachother 1 Answer