I wish to make a checker game. Now I am trying to implement draggable game pieces. Here are the scripts attached to my piece GameObject:
void OnMouseDrag(){
Debug.Log(gameObject.transform.position + " "
+ Camera.main.ScreenToWorldPoint(Input.mousePosition) + " "
+ Input.mousePosition + "
");
Vector3 convertedPos = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
0,
Camera.main.ScreenToWorldPoint(Input.mousePosition).z);
gameObject.transform.position = convertedPos;
}
I have an orthographic looking-downwards camera at (0,70,0).
When I tried to drag the pieces, in some cases it works fine. In other case the pieces moved are not the ones I dragged.
OnMouseDrag() will only be called if you clicked on the collider for that object, and that collider has to be the first collider at this position. Maybe you have an oversized collider on one or more of your objects?
– robertbu