How to detect the position of a texture that is clicked

Hi guys,

I’m very new to Unity and I have what seems to be quite a simple scenario, that I’m just not too sure how to solve.

I am making a 2D game and am building a menu system that has an image. The image is (for example) 1000px wide. I want to be able to click on anywhere within the image and ascertain the X position of the click (left click using the mouse). For example, if I clicked in the middle of the image, I’d know the X position was 500px.

I’ve heard of physics 2D ray caster and think it could help me here but I’m not sure how to implement this.

I have my main camera and I’ve added the physics raycaster 2d to it but not sure how to implement the code on the image to say, if I’m clicked get the position.

Thanks for any help in advance! :slight_smile:

Why don’t you use a button?

then, have some function like

public void OnButtonClicked(){
 float x = Input.mousePosition.x;
}

that you call on the button click event? Raycasting is really not necessary here.