1
\$\begingroup\$

I have a ball which has CircleCollider2D attached to it. It is also rigidbody. When it lands on a platform, it jumps . The platform is static, with Edge Collider attached. It also has a platform effector attached with "Use One Way" selected.

Most of the times it works fine, but some times, the ball lands on the platform and instead of bouncing, just sits on the platform.

When ball lands on the platform, following code is executed:

 rb.AddForce(upForce, ForceMode2D.Impulse);

upForce is Vector2(4,10);

image shown below:

enter image description here

Why is this happening?

\$\endgroup\$
  • 1
    \$\begingroup\$ Where are you calling the addforce method? Is it in OnCollisionEnter2D... If so,try changing it to OnCollisionStay2D. \$\endgroup\$ – Savlon Aug 16 '15 at 15:39
  • 1
    \$\begingroup\$ Nice catch. It occurs once in may be 200 attempts. Very difficult to reproduce. But OnCollisionStay2D will certainly take care of it. No way of validating, but it should \$\endgroup\$ – SanSolo Aug 16 '15 at 18:03
  • 1
    \$\begingroup\$ @Savlon please write it as answer. \$\endgroup\$ – SanSolo Aug 17 '15 at 6:42
0
\$\begingroup\$

As requested by SanSolo - Comment converted to answer.

If you are calling the AddForce method from OnCollisionEnter2D then it will only get called on the initial contact of both colliders. If for some reason, your collision fails to get recognised, your AddForce method will fail.

An easy way to fix this is to simply place your AddForce method inside of the OnCollisionStay2D method. This will ensure that your AddForce method gets called every time your colliders are in contact regardless of any unrecognised initial collisions.

\$\endgroup\$
1
\$\begingroup\$

Gravity in unity is a default of roughly 9. Are you simulating gravity, are you using the correct scale and have you tried adding more force?

\$\endgroup\$
  • \$\begingroup\$ He doesn't need gravity its rigidbody \$\endgroup\$ – Vadim Tatarnikov Aug 17 '15 at 7:15
  • 1
    \$\begingroup\$ Not my point. His force is only +10. As gravity is -9 I thought it may just not be enough force to move it \$\endgroup\$ – cfehunter Aug 17 '15 at 7:17
  • \$\begingroup\$ Oh yes that makes sense sorry \$\endgroup\$ – Vadim Tatarnikov Aug 17 '15 at 7:20
0
\$\begingroup\$

Try using AddForce() in the function OnCollisionStay2D() so that it is called every time there is a collision.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.