Private Vector3 lastpos = Vector3.zero;
private bool Istouch;
Use this for initialization
void Start () {
}
Update is called once per frame
void Update () {
Determine if the mouse is in the clicked State
if (Input.getmousebuttondown (0)) {
Istouch = true;
}
if (input.getmousebuttonup (0)) {
Istouch = false;
Lastpos Zero when you select the mouse
Lastpos = Vector3.zero;
}
In the click State, the object moves with the mouse
if (Istouch) {
referential screen coordinates.
Vector3 Screenpos = Camera.main.WorldToScreenPoint (transform.position);
if (lastpos! = Vector3.zero) {
Drag-and-drop processing
Mouse offset coordinates in one frame
Vector3 OffsetPos = Camera.main.ScreenToWorldPoint (New Vector3 (input.mouseposition.x,
INPUT.MOUSEPOSITION.Y,
SCREENPOS.Z))-Lastpos;
Transform.position + = OffsetPos;
}
Record Previous frame coordinates
Lastpos = Camera.main.ScreenToWorldPoint (New Vector3 (INPUT.MOUSEPOSITION.X,INPUT.MOUSEPOSITION.Y,
SCREENPOS.Z)); Convert to world coordinates
}
}
}
Unity3d Simple way for C # objects to move with objects