Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rmccabe82 · May 03, 2015 at 11:58 AM · raycastunity 4.6phyics

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! :-)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by siaran · May 03, 2015 at 01:09 PM

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rmccabe82 · May 03, 2015 at 01:22 PM 0
Share

Great! That might work for me! Would that give me the X pos relative to the object clicked? Like 500px being the position when half way is clicked in a 1000px width object? thanks!

avatar image siaran · May 03, 2015 at 01:51 PM 0
Share

no, that would give you the screen position of the mouse, but you should be able to convert those to the position relative to your button easily enough...

avatar image rmccabe82 · May 03, 2015 at 02:22 PM 0
Share

:-) have you don't this before? How do I convert?

avatar image siaran · May 03, 2015 at 02:47 PM 0
Share

You'll simply need to know the left-most (screen) x position of your button. Should be easy enough to obtain (if you don't know how, read the docs or google, that information should be easy to find.)

then you'd do something like

 float left$$anonymous$$ost = rectTransform.x - rectTransform.width/2; //Not actual correct code! don't use this.
 float x = Input.mousePosition.x - left$$anonymous$$ost;





avatar image rmccabe82 · May 03, 2015 at 02:48 PM 0
Share

Thanks very much for your help! :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to send a raycast to an object to give me ammo? 0 Answers

How to return UI element at mouse with RayCast 1 Answer

What does it mean if my RaycastHit.texturecoord changes dramatically on either side of adjacent triangle edges? 0 Answers

Why does this script I got from the Unity Document site does not work? 1 Answer

How to register if my Raycast hits nothing(Solved) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges