Hi everyone, is it possible to get the x/y coordinates of a pixel on a model from where you click on it?
for example, let’s say I have a low-rez texture on a flat plane, generated through code, measuring perhaps 16x16 pixels, and I click on the 3rd pixel from the left and the 10th pixel from the top. is there a way for unity to know those coordinates, so that I can use them, for example, to appear in the console?
In Update() would work. You’ll want to check if the mouse has been clicked with, say, Input.GetMouseButtonDown(0).
modelWasClicked was added under the assumption that Captain_Dando might end up processing the clicked coords after the if statement. Jessy’s right in saying it’s a bad idea to use that, you’re better off putting whatever click-handling code inside that if statement:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Vector2 clickedUvCoords; //will be set to where the user clicked on the model, in UV (texture) coordinates
Collider someCollider; //could just be this GameObject's collider, assign whatever you need
if (someCollider.Raycast(ray,hit,Mathf.Infinity)) {
clickedUvCoords = hit.textureCoord;
//your click-handling code here
}
//don't do anything after here that assumes the model's been clicked, unless you have a "return;" at the end of the above if statement, or wrap it in an "else {}"