- Home /
Dragging object with Mouse causes it to fly away
Hello Unity Community. I'm very new to Unity, and I'm having a bit of trouble with dragging an object with the mouse.
To explain what I am trying to do: I am making a Chess game in Unity. Right now I am just trying to move a GameObject via mouse dragging. I have looked up some solutions to the problem but they cause the object to move to a certain spot.
After browsing Unity Answers for a while, I tried out this code:
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.y = gameObject.transform.position.y;
gameObject.transform.position = point;
To illustrate the problem:
1) My Board in Game view:
2) Clicking on the pawn I have my script attached to (Row 2, Column 1):
3) Where did it go? Seems to have snapped in the center for some reason!
I modified the code from sirbluenotes' answer here. I tried the OP's final solution from the question but got the same problem.
I'm not sure if it's a problem with my code or where my camera is situated (just about 8 units above the game board). I have not tried to do this with Rays yet (Camera.ScreenPointToRay), but I may have to.
Sorry for the excess of images, and thanks in advance for your help!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Dragging UI Image by touch 3 Answers
Object drags wherever I touch on screen- Iphone-Unity 1 Answer
Checking if mouse dragged from one point to another 2 Answers