Return the position(x,y) of the pixel when Clicked a Sprite

Hi Guys, i have a Question i hope you could help me.
This is my problem, when i click an Sprite2D the mouse gives me WOLRD Coordinates, but i want the Sprite Coordinates.
Y have an 50x50 Sprite and i click on the middle of it.
I want a return that indicates me the coordinate of the sprite, that will be 24,24.
I Hope you understand.

Thnks Guys for your time. :slight_smile:

If you are willing to:

  • Supply the pixel width and pixel height to the script
  • Supply the units to pixels value in the spript
  • Use an Orthographic camera
  • Set the Pivot of the sprite to the Bottom left
  • Single texture (not an atlas)

Then you can do something simple like this:

#pragma strict
 var pixelWidth = 76.0;
 var pixelHeight = 150.0;
 var unitsToPixels = 100.0;
function OnMouseDown() {

    // assumes an orthographic camera
	var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
	pos.z = transform.position.z;
	pos = transform.InverseTransformPoint(pos);
	var xPixel : int = Mathf.RoundToInt(pos.x * unitsToPixels);
	var yPixel : int = Mathf.RoundToInt(pos.y * unitsToPixels);
	Debug.Log("("+xPixel+", "+yPixel+")");

The more you stray from the above list of criteria (or want to automate so that you don’t have to input the date) the more complicated the script will be to calculate the position. Note that using a Quad with a MeshCollider makes this functionality trivial.