Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by GetShr3kt · Aug 31, 2016 at 05:38 AM · uiraycastraycasting

What's an easy way to align a UI element with a Raycast?

I'm working on a photography type game and want to implement bonus scores being rewarded for things such as rule of thirds, which appears as a grid on the screen for the player to lineup. However, I can't seem to get the raycasts for where it should trigger as a rule of thirds hit to quite line up with the UI element.

Here's what I've been doing:

         RaycastHit hit;
         Ray photoRay = new Ray (transform.position, transform.forward);
         Ray rotRay1 = new Ray (new Vector3 (transform.localPosition.x - 4.5f, transform.localPosition.y + 2.5f, transform.position.z), transform.forward);
         Ray rotRay2 = new Ray (new Vector3 (transform.localPosition.x - 4.5f, transform.localPosition.y - 2.5f, transform.position.z), transform.forward);
         Ray rotRay3 = new Ray (new Vector3 (transform.localPosition.x + 5, transform.localPosition.y + 2.5f, transform.position.z), transform.forward);
         Ray rotRay4 = new Ray (new Vector3 (transform.localPosition.x + 5, transform.localPosition.y - 2.5f, transform.position.z), transform.forward);
 
         //Debug.DrawRay (cam.transform.position, Vector3.forward * focalLength);
         if (cameraOut && Input.GetMouseButtonDown (0)) {
              if ((Physics.Raycast (rotRay1, out hit, focalLength)) || (Physics.Raycast (rotRay2, out hit, focalLength)) || (Physics.Raycast (rotRay3, out hit, focalLength)) || (Physics.Raycast (rotRay4, out hit, focalLength))) {
                 if (hit.collider.tag == "Front") {
                     Debug.DrawRay (new Vector3 (transform.position.x - 3.2f, transform.position.y + 1.5f, transform.position.z), transform.forward);
                     print ("Rule of Thirds photo of animal front");
                 } else if (hit.collider.tag == "Back") {
                     print ("Rule of Thirds photo of animal back");
                 }
             }
             else if (Physics.Raycast (photoRay, out hit, focalLength)) {
                 if (hit.collider.tag == "Front") {
                     print ("Take photo of animal front");
                 } else if (hit.collider.tag == "Back") {
                     print ("Take photo of animal back");
                 }
             }
         }

The code isn't pasting very nicely but essentailly what I'm doing right now is assigning the starting position of the raycast via the transform.localPosition. No matter how much I change the values, it never seems to line up. Am I approaching this the wrong way or do I need to keep adjusting the position of the rays?

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Input problem 1 Answer

How to use both NGUI widgets and unity UI 0 Answers

UI Raycast to UI element position 0 Answers

Drag'n'Drop control Image raycast in code. 0 Answers

UI button and raycast script that I have made both aren't working to change my scene 1 Answer


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