# Unity Ball moving inside the surface

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;

}

}


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

Some suggestions:

1. Change your balls' mesh collider to a sphere 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

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

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.