- Home /
Drag object in unity 2d with moving camera
I want to drag my 2d object in unity2d using code with:
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position + Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(screenPoint.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
transform.position = curPosition;
}
Problem is that my camera is also moving along x axis so when I click on gameobject to drag it, it moves along x axis. I want to make this gameobejct static so it will be in its place and won't move? Any suggestions?
Comment
Your answer
Follow this Question
Related Questions
Touchscript drag constraints 1 Answer
Drag rigidbody to crosshair 0 Answers
Drag-and-drop dot on mobile 2D 0 Answers
Scale a Vector3 Force depending to alignment of a Drag Vector3 1 Answer
Keep vectors exactly 15 distance apart 2 Answers