Search Unity

[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:
    892
    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:
    23
    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,972
    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:
    23
    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,972
    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:
    23
    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:
    23
    Are there any news how to avoid the issues? Are working on it?
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    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.
     
    HECer likes this.
  14. MelvMay

    MelvMay

    Unity Technologies

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

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    HECer likes this.
  16. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    Perfect! Thank you a lot, great work!!

    In which unity version the fix has been applied?
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    The improvement has been applied to 2020.1 and after more QA it'll be considered for backporting to earlier versions.

    EDIT: There's a 2019.3 backport in review in progress.
     
    Last edited: Aug 30, 2019
    HECer likes this.
  18. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    Can you hit me when it is finally released in 2019.3? Or earlier versions?
    I am currently on 2019.1.14f1 on the game I am developing
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    Sure, I'll put a note on the case to reply here.
     
    HECer likes this.
  20. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    This has just landed in 2019.3.0b3 (not released yet).
     
    HECer likes this.
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    HECer likes this.
  22. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    2019.3.0b3 has now been released.
     
    HECer likes this.
  23. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    Awesome I will try it out!
    Hope 2019.2 is also ready soon.
     
  24. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    There's been a pull-request for the last week. It's waiting in the 2019.2 merge queue.
     
    HECer likes this.
  25. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    would be nice to integrate the fix in our currently finished game prototype we will finally send over to the publisher =)
     
  26. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    It's in the queue for 2019.2.7 which is due to be published on 26th September.

    Note: I have yet to see you confirm if this fixes your issue, still waiting. ;)
     
    HECer likes this.
  27. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    I tested my current project-configuration on 2019.3b. It worked very well, nothing shifted away anymore =) But the overall performance isn't as good as on the current version of 2019.2.3f1.
    It needs more testing for me and playing more with the configurations.
    I am keen to see the Version 2019.2.7 and to be able to use the fix :)

    Thank you a lot!
     
  28. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    I presume you don't mean performance of the constraints and other Unity stuff because the constraint performance now is a lot better.
     
    HECer likes this.
  29. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    @HECer The change just landed in 2019.2.7f2
     
    HECer likes this.
  30. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    MelvMay likes this.
  31. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    Ok i cannot find Unity 2019.2.7f2 in the repository of unity versions.
     
  32. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    Because it's not released yet. I believe it's due to be released this week.
     
    HECer likes this.
  33. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
  34. HECer

    HECer

    Joined:
    Mar 17, 2013
    Posts:
    23
    Didn't realize the problem yet.
    The new constraints are working great!
    The performance is a lot better! Thank you a lot!
     
    MelvMay likes this.
  35. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    The fix will be in shortly in all versions anyway so you'll probably not encounter it. Glad you're getting better results.
     
    HECer likes this.