Search code examples
unity-game-enginegame-enginegame-physics

Allow a rigid body to only move in the Y direction.


I am making a 2d game using Unity 4.3. I have a rigidbody2d object. When other physics2d objects hit the rigidbody2d object it moves a little bit in the x direction.

I don't want it to move in the x direction. I want to make the rigidbody2d object's x direction fixed. How can I achieve that?


Solution

  • Unfortunately, RigidBody2D is missing a key feature present in RigidBody (3D), constraints.

    A workaround could be to always lock your RigidBody2D's position on the X-Axis, although, this may lead to bugs in your physics:

    void Update() 
    {
        transform.position = new Vector3(STATIC_X, transform.position.y, transform.position.x);
    }
    

    Another option, would be to not use a RigidBody, and attempt to simulate the physics yourself.