Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AGirdeux · Mar 11, 2014 at 08:15 PM · addforcebounceheightsame

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image highpockets · Mar 11, 2014 at 08:17 PM 0
Share

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 );
avatar image highpockets · Mar 11, 2014 at 08:32 PM 0
Share

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

avatar image AGirdeux · Mar 11, 2014 at 10:14 PM 0
Share

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

avatar image highpockets · Mar 12, 2014 at 04:39 AM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Marzoa · Feb 14, 2020 at 10:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges