Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Physics - Rolling Ball

Discussion in 'Editor & General Support' started by Avoozl, Mar 3, 2012.

  1. Avoozl

    Avoozl

    Joined:
    Jan 13, 2012
    Posts:
    36
    Hello. I am hoping that someone here could help me resolve the following problem:

    1). I create a box game object, scaled and sized to act as a floor object.
    2). I create a sphere game object. I add a Rigid Body component to the sphere and place the sphere in mid-air above the box. I adjust no further settings.
    3). I add script to the box so that it rotates about its center accordingly as I press the WASD keys.

    Now, at runtime, if I use the WASD keys to angle the box BEFORE the sphere falls from its starting position and collides with the box, then the sphere correctly rolls off the box, as though it were a ball (as I was expecting). BUT, if I leave the box at its starting orientation and press nothing, the sphere collides with the box, as before, but will not roll off the box once I rotate it using the WASD keys.

    In short I want the ball to roll off the box in both cases.

    Can anybody help regarding this. Thank you in advance.
     
  2. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    So you're saying the sphere acts like its glued to the box and won't roll off? How steep is the angle, is the sphere moving with the box as you rotate the box?
     
  3. Avoozl

    Avoozl

    Joined:
    Jan 13, 2012
    Posts:
    36
    The sphere acts like a ball and rolls off the box, IF I rotate the box BEFORE the sphere falls from its starting point in mid-air and collides with the box. Thus, it works if the box is already angled as a ramp before the sphere collides.

    The sphere seems to freeze and remain unaffected by the box if I wait until the sphere lands on the box and loses momentum before rotating it.
     
  4. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    What I'm asking is, when the box is flat, you drop the sphere, then you rotate the box, this should affect the ball, the collider of the box should hit the sphere and cause a collision, if maybe only a small one. If the sphere is pinpoint in the middle of the box it may not actually be causing a collision. The reason I ask this is that the sphere may be going to sleep and if the box doesn't collide with it when you start to rotate it, the sphere won't wake up which will have it remain sleeping, which means doing nothing. Try moving your sphere away from the middle if it is centered so that when you rotate the box it will come up into the sphere and cause a collision. If the box swallows up part of the sphere and nothing is happening this gives good clues that for whatever reason the two objects are not colliding.
     
  5. Avoozl

    Avoozl

    Joined:
    Jan 13, 2012
    Posts:
    36
    Thank you for the reply. The position at which the sphere collides with the box does not seem to make a difference. If the sphere lands at the middle of the box or at the edge, it appears to 'go to sleep', as you say, almost as soon as it lands. If I rotate the box when the sphere is asleep in this way, the box passes through sphere as though it did not exist.

    I can solve this issue by applying a ConstantForce component to the sphere instead of a RigidBody. When I do this, the sphere will always react to the movement of the box. Is this an appropriate solution however, or is there something I am missing regarding rigid bodies?
     
  6. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    257
    Go into the Physics settings and turn the sleep settings down to zero. If this fixes your problem then you know this is the cause.
     
  7. Avoozl

    Avoozl

    Joined:
    Jan 13, 2012
    Posts:
    36
    Thank you both for the help. I can confirm that the issue must be sleeping because the following code ensures the ball behaves correctly:

    Code (csharp):
    1. function Update()
    2. {
    3. if(rigidbody.IsSleeping())
    4. rigidbody.WakeUp();
    5. }
    The Sleep settings in the Physics Settings menu had no effect on this issue, however.

    Thanks again for the help.
     
  8. VeraxOdium

    VeraxOdium

    Joined:
    Jul 2, 2011
    Posts:
    233
    From the manual:

    Kinematic rigidbodies wake up sleeping rigidbodies. Static Colliders do not. If you have a sleeping rigidbody and you move a static collider (A collider without a Rigidbody attached) into the rigidbody or pull it from underneath the rigidbody, the sleeping rigidbody will not awake. If you move a Kinematic Rigidbody out from underneath normal Rigidbodies that are at rest on top of it, the sleeping Rigidbodies will "wake up" and be correctly calculated again in the physics update. So if you have a lot of Static Colliders that you want to move around and have different objects fall on them correctly, use Kinematic Rigidbody Colliders.

    Kinematic rigidbodies - they will not be calculated during the physics update since they are not going anywhere. If you move a Kinematic Rigidbody out from underneath normal Rigidbodies that are at rest on top of it, the sleeping Rigidbodies will "wake up" and be correctly calculated again in the physics update. So if you have a lot of Static Colliders that you want to move around and have different objects fall on them correctly, use Kinematic Rigidbody Colliders.
    _____________________________________________

    So you could attach a rigidbody to your box and then check isKinematic, this will automatically wakeup the sphere without any coding hacks whenever there is a collision.
     
  9. Avoozl

    Avoozl

    Joined:
    Jan 13, 2012
    Posts:
    36
    Ah ha :) Thank you. That solved the issue.