c# - Image jumps to its center point when dragged with a mouse -


hoping has quick answer 1 haven't been able figure out. rather have image jumping center on mouse cursor, i'd able drag image place on image without jump. know has referencing mouse position against image or reseting origin point of image mouse location, don't know how code it. has done already? using c#.

vector3 partspanelscale; public vector3 buildpanelscale;  public transform placeholderparent = null; public transform parenttoreturnto = null;  gameobject placeholder = null;  public gameobject animalpart;  public gameobject trashcan;  public gameobject partspanel; public gameobject partswindow; gameobject buildboard; gameobject draglayer;  private float _mx; // holds current eventdata.position.x private float _my; // holds current eventdata.position.y private float _pmx;// holds previous eventdata.position.x private float _pmy;// holds previous eventdata.position.y  void start () {     draglayer = gameobject.findgameobjectwithtag("draglayer");     buildboard = gameobject.findgameobjectwithtag("board");     partspanel = gameobject.findgameobjectwithtag("parts");     partswindow = gameobject.findgameobjectwithtag("partswindow");     trashcan = gameobject.findgameobjectwithtag("trash"); }  #region ipointerclickhandler implementation  public void onpointerclick (pointereventdata eventdata) {     if(transform.parent.gameobject == buildboard)     {         transform.setaslastsibling();     } }  #endregion  #region ibegindraghandler implementation  public void onbegindrag (pointereventdata eventdata) {     // each frame updates current position of mouse.     _mx = eventdata.position.x;     _my = eventdata.position.y;      // create placeholder gap , hold correct position in layout     placeholder = new gameobject();     placeholder.transform.setparent(transform.parent);     placeholder.transform.setsiblingindex(transform.getsiblingindex());      if(transform.parent.gameobject == partspanel)     {         partspanelscale = transform.localscale;     }      parenttoreturnto = transform.parent;                                    // store current parent location     placeholderparent = parenttoreturnto;                                   // set placeholder gameobject transform      getcomponent<canvasgroup>().blocksraycasts = false;                     // turn off image raycasting when dragging image in order see what's behind image             }  #endregion  #region idraghandler implementation  float distance = 0;    public void ondrag (pointereventdata eventdata) {      // divided difference 6 reduce speed of dragging.     transform.position = new vector3         (             (_pmx - _mx)/6 + transform.position.x,             (_pmy - _my)/6 + transform.position.y,             distance             );      // vector3 mouseposition = new vector3(eventdata.position.x, eventdata.position.y, distance);     // vector3 objposition = camera.main.viewporttoscreenpoint(mouseposition);     // transform.position = mouseposition;                                  // set object coordinates mouse coordinates      if(transform.parent.gameobject == partspanel)     {         transform.setparent(draglayer.transform);                           // pop object draglayer move object out of partspnael     }      if(transform.parent.gameobject == buildboard)     {         // constrain drag boundaries of buildboard code     } }  #endregion  #region ienddraghandler implementation  public void onenddrag (pointereventdata eventdata) {     // end of drag. set previous position.     _pmx = _mx;     _pmy = _my;      transform.setparent(parenttoreturnto);                                  // snaps object orginal parent if dropped outside of dropzone     transform.setsiblingindex(placeholder.transform.getsiblingindex());     // returns card placeholder location      getcomponent<canvasgroup>().blocksraycasts = true;                      // turn raycast on     destroy(placeholder);                                                   // kill placeholder if object hits drop zone or returns parts panel      if(transform.parent.gameobject == buildboard)     {         // debug.log ("your sprite on " + transform.parent.name);          transform.localscale = buildpanelscale;         transform.setaslastsibling();                                       // place last piece on top     }      if(transform.parent.gameobject == partspanel)     {         transform.localscale = partspanelscale;     } }  #endregion 

to drag correctly need both previous , current position of mouse.

because each time move mouse image should move bit (at x , y coordinate) previous position.

if use current position , directly set image resault jump on screen.

you have transform transformation of image getting differences of previous , current position (x2 - x1 , y2 - y1).then set final transformation image transform.

you need mouseover event update positions. (ormousemove)

private double _mx; // holds current eventdata.position.x private double _my; // holds current eventdata.position.y private double _pmx;// holds previous eventdata.position.x private double _pmy;// holds previous eventdata.position.y  // each frame updates current position of mouse. private void mouseover(pointereventdata eventdata) {     _mx = eventdata.position.x;     _my = eventdata.position.y; }  public void ondrag (pointereventdata eventdata) {     transform.position = new vector3d((_pmx - _mx)/6 + transform.position.x,                                       (_pmy - _my)/6 + transform.position.y, distance);     // divided difference 6 reduce speed of dragging.      //...      // end of drag. set previous position.     _pmx = _mx;     _pmy = _my; } 

note class names , events have used may differ unity3d.

if drag right left image goes left right replace _pmx - mx _mx - _pmx.

if drag down image goes down replace _pmy - my _my - _pmy.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -