Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

[Help] lock position but not rotation? Rigidbody2D

Discussion in '2D' started by sebasrez, Mar 18, 2015.

  1. sebasrez

    sebasrez

    Joined:
    Oct 29, 2012
    Posts:
    30
    hey yall, anyone know of a way to lock the position of a rigidbody 2D? Slider joint locks the rotation and there is still a drag in x,y. Distance joint also has a drag that can only be set to 0.005.

    Anyone figure out a way to achieve this?

    Edit: The child of the parent should move with the parent but not have that drag shown in the video. It should stay locked in the X and Y but still be able to rotate

    video showing hinge joint
    Distance joint has the same effect.
     
    Last edited: Mar 19, 2015
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    891
    If you don't want an object that doesn't move at all, can't you just set it to static?
     
  3. sebasrez

    sebasrez

    Joined:
    Oct 29, 2012
    Posts:
    30
    I would like it to be able to rotate and move with the player but not to have that drag in the x and y
     
  4. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    I'm a little confused by your question, but maybe a wheel joint 2D will get you what you want?
     
  5. sebasrez

    sebasrez

    Joined:
    Oct 29, 2012
    Posts:
    30
    My question is if anyone knows how to attach a child rigidbody on a parent rigidbody and have it follow the parent without having a drag shown in the video. When I use joints to keep it in place it has a drag as shown and the wheel joint 2D also drags it as well.
     
  6. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    Make sure both rigid bodies have their linear drag set to the same value. On the wheel joint set the suspension damping ratio to 1 and the frequency to 1000000.
     
    theANMATOR2b likes this.
  7. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    15
    I still have this problem in Unity 2019.1.12f1, how to resolve it?
    I completely freeze the position of the rigid body2D, i just want it to rotate on collision.
    But the rigidbody2D jumps a little away on collision and back again. It's wired....
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,832
    Can you be more specific on your setup? I am guessing you mean you're using the constraints to freeze both X & Y? In the end constraints are another joint so are solver like other joints. You are free to bump-up the number of iterations the solver uses in the physics settings and not use the defaults.

    It would be better first though to understand what kind of collision is causing the constrained body to move.
     
  9. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    15
    Sry i just now saw the comment of you...

    Yes I am using the constraints to freeze the position completely.
    Thank you for your help, maybe bumping up iterations could help.
    Is there no possibility to simulate a 2D object that only can rotate without moving at all?

    I am hitting a gear (just a collider and a rigidbody (set xy constraints to fixed)) with a 2D ragdoll (with colliders, hinges, rigidbodies) very hard. On the collision the gear moves slightly from place and back again and starts to rotate, based on the degree and force I am hitting it from.
    In my case I want to stay the gear perfect in place without moving at all.

    Hope you have some ideas to fix the issue.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,832
    Yes, it's just that something is obviously going wrong in your project. We need to find out what.

    Not without having more of an idea of why the constraint is moving. If you want upload an example so I can take a look then that would be okay too. You can upload that here.
     
  11. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    15
    Hey, thank you for the support, I uploaded a sample (Unity 2019.1.12f1) where you should see the issue.
    Just start the game and press a couple times with your mouse. You will notice the gears will start to drag a little.

    I tested it also with a single, normal collider with a rigidbody, the issue doesn't come up.
    Only if I use the ragdoll.
     
    MelvMay likes this.
  12. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    15
    Are there any news how to avoid the issues? Are working on it?
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,832
    I apologise, I've been swamped with other stuff although I did take a quick look. I'll look at it in more detail later today or in the morning.

    It's unlikely however that you'll ever get it so that it doesn't move at all as all of Box2Ds joints are "soft" and not totally rigid.

    As much as it pains me, I suggest as a workaround you could use the following script attached to a gear:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ConstrainedPosition : MonoBehaviour
    4. {
    5.     private Rigidbody2D m_Rigidbody;
    6.     private Vector3 m_ConstrainedPosition;
    7.  
    8.     void Start()
    9.     {
    10.         m_Rigidbody = GetComponent<Rigidbody2D>();
    11.         m_ConstrainedPosition = transform.position;
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         m_Rigidbody.velocity = Vector2.zero;
    17.         m_Rigidbody.position = transform.position = m_ConstrainedPosition;
    18.     }
    19. }
    20.  
    The downside to the code above is obviously having to add a script but it'll also keep the body awake because setting its position does that. For a bunch of gears however I don't see that as a performance problem.

    I've seen this issue mentioned several times now and in some cases a minor bump in solver iterations fixes it so it's not visible however in your case it's clearly evident. I would ask that you submit this as a bug report with the same project attached. That way you can not only track the change but I can also use it to look at constraining the position not using joints i.e. the solver. If you don't mind doing this then please send me the case number as soon as you have it.

    Thanks.
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,832
    Just wanted to bump this thread; if you can submit a bug report I can use it to issue a fix.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,832