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. Dismiss Notice

Rotating an object with a rigidbody2d component gets stuck.

Discussion in '2D' started by Esgeroth, Apr 29, 2014.

  1. Esgeroth

    Esgeroth

    Joined:
    Apr 29, 2014
    Posts:
    6
    I have a gameobject with a rigidbody2d component set to rotate in its update function. The object will make a few rotations and then will get stuck for a few seconds. It starts vibrating in place for a few seconds and then starts rotating again.
    I am new to doing anything in unity so I don't know if I am using the components incorrectly or if this is possibly a bug.

    To reproduce the behavior:
    In a new project create a new cube game object.
    Create a new c# script and in the update function add: transform.rotate(1,0,0);
    Add the script to the cube.
    Remove the cubes Box collider component.
    Add a rigidbody2d component and set gravity to 0;
    Then play the scene.

    I've done this on 2 computers both with the latest version of unity (4.3.4).
    I can make a short video of what happens if necessary but the above steps seem to reproduce the effect.
     
  2. playmonkey1

    playmonkey1

    Joined:
    Sep 20, 2013
    Posts:
    60
    I don't think u you can mix 2D Physics components in a 3D gameobject. If you select a rigidbody component the cube will rotate normally.
     
  3. Esgeroth

    Esgeroth

    Joined:
    Apr 29, 2014
    Posts:
    6
    As far as I have been able to gather Unity being a 3d engine only handles 3d objects. The 2d physics components such as the rigidbody2d and the boxcollider2d will constrain the objects movements on two axis making the physics calculations 2d. If you can't add a 2d physics component to a 3d object then what would you add it to? Even a plane with a texture applied to it is still a 3d object. Of course, I could be wrong.

    My goal is to have moving spheres that can collide with each other without moving closer or further away from the camera. Using the rigidbody2d and the circlecollider2d components achieves this wonderfully. Until I tried rotating the spheres, then I get the behavior described above. I specified cubes above as it is easier to see them rotating in a test example.
     
  4. playmonkey1

    playmonkey1

    Joined:
    Sep 20, 2013
    Posts:
    60
  5. Esgeroth

    Esgeroth

    Joined:
    Apr 29, 2014
    Posts:
    6
    A sprite is just a special texture that is still applied to a 3d object. A 3d object that is placed under control of the 2d physics system. Which is what I want and is working just fine with 3d spheres. The problem arises when trying to rotate said 3d object.
     
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    I had a similar issue that went away when I put the code in FixedUpdate instead of Update. I'm not sure of the exact parameters of the problem, but if you're updating the transform of a Rigidbody2D, it seems to me that you need to stick to FixedUpdate, even when those updates aren't strictly physics themselves.
     
  7. Esgeroth

    Esgeroth

    Joined:
    Apr 29, 2014
    Posts:
    6
    The problem seems to be even worse if I use FixedUpdate. Once the object gets stuck it stays there instead of continuing after a second.
     
  8. playmonkey1

    playmonkey1

    Joined:
    Sep 20, 2013
    Posts:
    60
    It works for me if u insert the transform in both update and fixedupdate? Def. not an ideal solution tho
     
  9. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    This doesn't happen to me when using a value of 1, but if I lower the value, it suddenly starts happening, whether it's in Update or FixedUpdate (it's worse in fixedupdate)

    Weird bug that it stops at lower speeds.

    If I change my TimeStep to a faster value, like 0.01666667 so that my FixedUpdate() runs at 60fps, it allows me to go to lower speeds without issue, but it still happens at even lower speeds. It's like the rigidbody wants to snap back to a 2D position if it hasn't been rotated enough.

    Though, using Rotate on a rigidbody is bad practice anyways, and the 2D system isn't meant to rotate in that direction. Might I ask why you need to rotate a 2D rigidbody in this way?
     
    Last edited: Apr 30, 2014
  10. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    I think Invertex is right. I hadn't realized you're rotating on an axis that doesn't rotate in 2D physics. So, any time physics are applied, you're losing that rotation? This matches your symptoms: in Update, you can rotate it some updates (vibration), and in FixedUpdate it doesn't work at all.
     
  11. Esgeroth

    Esgeroth

    Joined:
    Apr 29, 2014
    Posts:
    6
    I still don't see what the problem would be when rotating an object with a rigidbody2d component. After all, they are still 3d objects. I only want them to behave as 2d objects during collisions so that they don't move on the x axis. The only problem would arise if you try to mix objects with rigidbody and rigidbody2d components, which I am not.
    I have some spheres that I would like to have rotating and colliding with each other. This is working fine except that the stop rotating, even without any collisions.

    In any case I submitted a bug report and I got an email back that the issue has been reproduced and has been sent to the developers for resolution.

    I have been trying to get the same effect by using a rigidbody component and using constraints so that it does not move on the x axis. But every once in a while one of those spheres floats away in the x direction.
     
  12. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    And I don't see how you're expecting a physics-enabled object to rotate in three dimensions when you've in effect told the physics engine to only allow rotation in the Z-axis.

    So... Why not make the 3d mesh on a child object and rotate that object in its local space? Simple.
     
  13. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Rotate is supposed to be for objects that don't have physics on them. When you Rotate an object that has a rigidbody, it's not working with the physics system, it's doing something to the object that the physics update suddenly has to react to like "wtf just happened, why is the object suddenly rotated when no forces were applied to it? I have to do something about this!". This is the same reason why you get jitter if you translate a rigidbody instead of moving it with forces, it's the same reason you get penetration between rigidbodies if you translate them.

    Instead of using Rotate, you could perhaps set the rotation directly each frame instead with transform.rotation, inside of a Coroutine that loops the value back around once it reaches the 360 mark.

    But Pyrian's suggestion is probably best, make the mesh a child object of your 2D physics object, and simply rotate that mesh object.

    I'm a bit confused about what you're trying to achieve here, gameplay mechanic wise. Do you want a rigidbody object to absolutely stay in one place, but still affect colllisions? If so, simply set the rigidbody to "isKinematic" and it won't move unless you command it to.
     
  14. Esgeroth

    Esgeroth

    Joined:
    Apr 29, 2014
    Posts:
    6
    The 2d physics engine does not disallow rotation on any axes. It likely does not even acknowledge the existence of the third axis. It should rotate in three dimensions because it is still a 3d object. The issue I initially described is not even happening as a result of any physics manipulation or collision. It happens with only a single object in the scene. The unity team has already acknowledged this as a bug regardless of whether or not this is the "right" way of doing things.

    The objects are moving but I would like them to appear 2d from the cameras perspective. That is they should not get any closer or further away from the camera in the x direction. That is why I used the rigidbody2d as that is what it accomplishes. It's just some balls that move around and bounce off of each other. I thought I would be able to use a rigidbody with a constraint on the x axis to accomplish this. It starts out working very nicely but every once in a while two of those spheres will bounce off of each other and one of them will fly toward or away from the camera. With this method I used rigidbody.addtorque and not tranform.rotate.

    I will try adding the sphere mesh as a child of a physics object. That does sound like it would be the correct solution.