Search code examples
unity-game-engineunity3d-2dtools

how to make sprite fall straight down in unity2d (and not move from its column)?


How can I make sprites fall straight down, and resist any sideways motion whether by dragging or physics?

My code instantiates a prefab at runtime which has an attached script.

The script runs the following function after a while:

void addSliderJoint() {
    myVerticalSlider = gameObject.AddComponent<SliderJoint2D> () as SliderJoint2D;
    myVerticalSlider.angle = 90;
}

After this functions runs, I'd like the sprites to only move vertically, but they seem happy to move without restriction. I can "see" the SliderJoint2D in the inspector during runtime, and if I change its settings in the GUI, the sprites suddenly respond to it.

Sample repo available at https://github.com/thunderrabbit/finna-be-octo-wallhack


Solution

  • I just added this to your OnMouseDrag() method

    // Set the Position
        Vector2 newPos = new Vector2(startPos.x, startPos.y + dir.y);
        transform.position = newPos;