Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rigidbody 2d constraints

Discussion in '2D' started by Minimally, Nov 14, 2013.

  1. Minimally

    Minimally

    Joined:
    Jun 18, 2011
    Posts:
    5
    Playing around with Rigidbody2d I noticed there aren't options to freeze rotation/position like there are on a Rigidbody. I'm not too familiar with what's possible with Box2d so I don't know if this will eventually make it into Unity's implementation of it, but I'm wondering if there are any workarounds to setting up constraints on a Rigidbody2d (aside from not using it).
     
  2. straxusii

    straxusii

    Joined:
    Aug 13, 2013
    Posts:
    1
    I was wondering this too whilst starting to play around with the 2d widgets this morning, I suspect the new way of achieving this is to attach a sliderjoint2d but I don't have a working example yet.

    You can stop rotation by selecting 'Fixed Angle' in the rigid body 2d component.
     
    Last edited: Nov 14, 2013
  3. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Noticed the same thing and wondered myself... I'm hoping it will be added, but like you said, fixed angle covers the rotation... as far as position locking is concerned I suppose the only way right now would be to lock it via code in an update function or something.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,315
    Unfortunately yes, Box2D only provides constraints in the form of joints. It would be possible for us to internally add a SliderJoint2D for the X and/or Y axis to offer such a constraint but this wasn't something we were considering for the 4.3 release.

    I'm not sure if we'd really want to do that in the future but if there was a significant need for it then maybe so.

    Hope this helps.
     
  5. luispedrofonseca

    luispedrofonseca

    Joined:
    Aug 29, 2012
    Posts:
    938
    I'm looking for a solution to constraint a rigidbody2d but I'm a bit lost.

    Can someone please expand on the SliderJoint2D solution mentioned here?
     
  6. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
  7. Ash-Blue

    Ash-Blue

    Joined:
    Aug 18, 2013
    Posts:
    102
    Here is a script you can pop on any object and easily choose which axis to lock.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. enum AxisDirection {
    5.     x, y
    6. }
    7.  
    8. [RequireComponent (typeof (SliderJoint2D))]
    9. public class AxisLock : MonoBehaviour {
    10.     [SerializeField] AxisDirection lockAxis;
    11.  
    12.     void Awake () {
    13.         SliderJoint2D slider = GetComponent<SliderJoint2D>();
    14.  
    15.         slider.connectedAnchor = new Vector2(transform.position.x, transform.position.y);
    16.         slider.collideConnected = true;
    17.  
    18.         if (lockAxis == AxisDirection.x) {
    19.             slider.angle = 90;
    20.         } else {
    21.             slider.angle = 0;
    22.         }
    23.     }
    24. }
     
    moghes and OceanBlue like this.