Search Unity

Inconsistent 2D Sprite Dragging

Discussion in 'Scripting' started by Coops1979, Nov 23, 2014.

  1. Coops1979

    Coops1979

    Joined:
    Nov 23, 2014
    Posts:
    2
    Hi,

    I've created a simple 2D game with drag and drop sprites, I want to use both mouse and touch input to move the sprites on the screen, this script works however the item being dragged often jumps to a specific position within the view, it doesn't matter what item is being dragged but they always seem to skip to the same position. This is the only positional script in the scene, has anyone experienced the same?

    Code (CSharp):
    1. // Use this for initialization
    2.     void OnMouseDrag() {
    3.         hit = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0f);
    4.         transform.position = new Vector3(hit.point.x, hit.point.y, -5f);
    5.     }