Skip to main content


Showing posts from January, 2015

Raycast in unity2D using mouse Position

Hi Guys, today i will be showing, How to Raycast in unity2D using mousePosition. There are lots of solution present online but they miss one important point, i.e. setting mousePosition.z to the distance between your camera and the object in the scene, while using ScreenToWorldPoint.

Code :


voidUpdate () {

Vector3mousePos = Input.mousePosition;
mousePos.z = 10;

Vector3screenPos = cam.ScreenToWorldPoint(mousePos);

RaycastHit2Dhit = Physics2D.Raycast(screenPos,;

print (;


If you like this post and want to contribute Please click on the link given below.