Skip to main content

Posts

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 :

   publicCameracam;

//Updateiscalledonceperframe
voidUpdate () {

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

Vector3screenPos = cam.ScreenToWorldPoint(mousePos);

RaycastHit2Dhit = Physics2D.Raycast(screenPos,Vector2.zero);

if(hit)
        {
print (hit.collider.name);
        }

    }




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

https://www.paypal.me/Sushanta1991