0
\$\begingroup\$

My ball is jumping on a 3d cylinder, and sometimes the ball gets stuck inside the cylinder. The ball has a rigid body and it's continuously dynamic. The circle also has a mesh collider. Here is the code to make the ball jump:

void Update () {

    if(Mathf.Sign(rg.velocity.y)==-1){

        isforceApplied=true;
    }



}

void OnCollisionEnter(Collision collisionInfo)
{


    if(isforceApplied==true){

        isforceApplied=false;
        rg.AddForce(transform.up*forceAmount,ForceMode.Impulse);

    }


}

enter image description here

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't think it is the issue here, but using FixedUpdate when dealing with physics will save you a lot of headaches :) \$\endgroup\$ – Leo Mar 5 '18 at 12:36
1
\$\begingroup\$

Some suggestions:

  1. Change your balls' mesh collider to a sphere collider.
  2. Instead of bouncing your ball with code i'd just add a physics material to your collider.
  3. If your ball is passing through the floors and it has a rigid body and a sphere collider (is Trigger = false) it could be down to an animator, if you're using one.
  4. Is your mesh collider Convex?
  5. Is your collider to the correct size?
  6. check the mesh collider on your floor and see if thats set to convex.

Good luck

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ yes its convex is checked \$\endgroup\$ – user3699039 Mar 6 '18 at 7:13
  • \$\begingroup\$ bounce material can't make always a specific constant height jump.. \$\endgroup\$ – user3699039 Mar 6 '18 at 7:14
  • \$\begingroup\$ Can you specify what the solution was and mark a correct answer, you'll be helping the next guy to have your problem \$\endgroup\$ – Nilmag Mar 6 '18 at 9:25
  • \$\begingroup\$ Second was more close the solution of problem \$\endgroup\$ – user3699039 Mar 6 '18 at 11:55
  • \$\begingroup\$ I mean, mark it as the solution. Click the green tick. \$\endgroup\$ – Nilmag Mar 6 '18 at 12:06
1
\$\begingroup\$

Here are a few other checks and changes you could do...

As was said by Nilmag, use a sphere collider set to the same radius as the sphere mesh, instead of a mesh collider.

You should also change Update() to FixedUpdate().

Within OnCollisionEnter(...), you could loop through the collisionInfo.contacts array, and check to make sure that one of the contacts.normal.y is greater than something like 0.5f. This would make sure the sphere would only jump if it's made contact with the ground. However, if you want the sphere to jump from any surface it collides with, you could skip the loop through the contact points, and change 'transform.up * forceAmount' to 'collisionInfo.contacts[0].normal * forceAmount'.

Otherwise, make sure that transform.up is always pointing up. For instance if transform is from the object with the rigidbody attached to it, it might rotate if there are no rotation constraints on it.

| improve this answer | |
\$\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.