- Home /
How to put enough force to a object so it bounces to the same height always
Im working with 2D physics. I have a ball with a Circle Collider 2D(2D physics material - friction=0,bounciness=1) and a rigidbody2D attached to it(mass=1,drag=0,gravity=1) The ground has a BoxCollider2D(no physics material no rigidbody)
I want the ball to bounce off the ground with Y velocity 7
So I set up a OnCollisionEnter2D function where I add more force to the ball or less force depending on how fast its going. the problem is that this method makes tresholds between velocities.
I need a code which gets the Y velocity of the ball and somehow calculates the ammount of force needed to add to the ball so the bounce off velocity will be 7 When I jump on a higher ground the velocity at the collision is less so it needs more force. When I jump from high ground to lower ground I need to add negative force to the ball so the bounce off velocity will be 7
this is my code
void OnCollisionEnter2D(Collision2D Col)
{
if(rigidbody2D.velocity.y<7.1)
{
if(rigidbody2D.velocity.y<5)
{
//Ball Y velocty less then 5 add more force
rigidbody2D.AddForce(Vector2.up*2500f*rigidbody2D.velocity.magnitude*Time.deltaTime);
}
else
//Ball Y velocty less then 7.1 but more than 5 add less force
rigidbody2D.AddForce(Vector2.up*100f*rigidbody2D.velocity.magnitude*Time.deltaTime);
}
if(rigidbody2D.velocity.y>6.9)
{
//Ball Y velocty more then 6.9 add negative force
rigidbody2D.AddForce(-Vector2.up*400f/rigidbody2D.velocity.magnitude*Time.deltaTime);
}
}
Help plssss
Why don't you directly modify the velocity vector ins$$anonymous$$d? Since you know that it has to be seven, just do the following:
rigidbody2D.velocity( 0, 7 );
Anyways, the reason that your code isn't working is because it is only adding force on one frame. Therefore, it will be hard to ever get close to an exact velocity. You could do it smoothly over more frames by having a max bounce height ins$$anonymous$$d. Once a bounce occurs, you could calculate the difference from the ball to your max height and apply force using the y difference:
float maxY = 20;
void OnCollisionEnter2D(Collision2D Col)
{
StartCoroutine( Bounce() );
}
IEnumerator Bounce()
{
bool inRoutine = true;
while( inRoutine )
{
float speed = 4;
float diff = maxY - rigidbody2D.velocity.y;
if( diff > 0 )
{
rigidbody2D.AddForce( 0, diff * speed );
}
else
{
inRoutine = false;
}
yield return null;
}
yield return null;
}
I did it in a Coroutine, so it won't be checking the conditions every frame
On line
rigidbody2D.AddForce( 0, diff * speed );
it says: No overload for method 'AddForce' takes '2' arguments
Im not sure what that means but shouldn't there be a vector2.up diff speed or something?
Ive tried that it fixed the error but the ball flew away very quickly and it didnt come down
Ive tried vector2.up diff speed * Time.deltatime but then the ball was getting slower and slower
oops...
rigidbody2D.AddForce( new Vector2( 0, diff * speed ) );
I forgot to put the vector2 in there. Sorry.
Also, I forgot to put a calculation in there if the platform has a higher y value than 0. You could do this on the line after the "bool inRoutine = false;", on line 14. Just do :
float yValue = maxY + transform.position.y;
That should do it. Sorry about the error, I did the code quick on my phone
Answer by knunery · Mar 11, 2014 at 08:30 PM
If I understand your problem correctly then you could do this.
void OnCollisionEnter2D(Collision2D Col)
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0); // set y velocity to zero
rigidbody2D.AddForce(new Vector2(0,1000)); // some constant force here
}
Answer by AGirdeux · Mar 12, 2014 at 05:30 AM
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0); // set y velocity to zero
rigidbody2D.AddForce(new Vector2(0,350)); // some constant force here
nice this fixed it
Thank you
Answer by liveswithoutfear · Sep 14, 2018 at 03:52 PM
@knunery I used this script to make an object in my game keep the same velocity and it worked great, however when the object collides with a wall it continues to gain height up the wall, like a bouncing Spider-Man. What can be added to the script to prevent the object from interacting with the walls in such a way? So it only reacts to the floor? At the moment this is true with any other object with a collider attached to it.
You could probably use a navigation mesh, though you write this a long time ago so I guess you had it sorted out already.
Your answer
Follow this Question
Related Questions
Ball's bouncing height is not same all the time. 0 Answers
Bouncing Ball Game 3 Answers
Rigidbody2D.Addforce results in unrealistic force 1 Answer
Bouncing Toward Target Point 0 Answers
OnTriggerEnter AddForce 1 Answer