← All dev blog posts

Mouse Position - Unity

3 June 2020

Code to get the Mouse Position in a 2D app

Vector3 mPos = Input.mousePosition;
mPos.z = -Camera.main.transform.position.z;
Vector3 targetPos = Camera.main.ScreenToWorldPoint(mPos);

Then to make something face towards it.

yourGameObject.transform.LookAt(targetPos);