Unity:How to Get Position of Mouse Left Click

Mouse Button Down in Unity2D:

In Your Update Method Write the following code and attach to empty object or Maincamera

if (Input.GetMouseButtonDown(0))

{

//Input.GetMouseButtonDown(0)) tell us where is mouse left click happens for rightclick //its Input.GetMouseButtonDown(1))

//Input.mousePosition tells where the position for left click and convert it into //ScreenToWorld point.

Vector3 pos = camera.ScreenToWorldPoint(Input.mousePosition);

//RayCast cast a ray camera to screen and it works..

RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);

if (hit != null && hit.collider != null)

{

Debug.Log("I'm hitting On Windows PC " + hit.collider.name);

player.transform.position = hit.point;

}

}

Comments