Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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 AmasterAmaster · Jul 30, 2015 at 12:03 PM · physicsraycastrendertextureclickselection

Is there a way to click on a render texture to select soemthing within the view?

Basically I was thinking if there was a way to click/select a certain part of a render texture that selects the correct selection within that view provided. How would that work? Would I have a Physics.Raycast within another Physics.Raycast call or am I forgetting something here?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Nischo · Jul 30, 2015 at 12:20 PM

So i tried it in a simple test scene because the problem intrigued me. My setup is rather simple and looks something like this.

alt text

a MainCamera looking at a Plane that uses a Render Texture in its Material. And the PortalCamera which looks at 2 primitives and renders it into the before mentioned render texture. On the Main Camera i attached this script.

 public class RayCast : MonoBehaviour {
 
     public Camera portalCamera;
 
     void Update () 
     {
         if(Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
             // do we hit our portal plane?
             if (Physics.Raycast(ray, out hit)) 
             {
                 Debug.Log(hit.collider.gameObject);
                 
                 
                 var localPoint = hit.textureCoord;
                 // convert the hit texture coordinates into camera coordinates
                 Ray portalRay = portalCamera.ScreenPointToRay(new Vector2(localPoint.x * portalCamera.pixelWidth, localPoint.y * portalCamera.pixelHeight));
                 RaycastHit portalHit;
                 // test these camera coordinates in another raycast test
                 if(Physics.Raycast(portalRay, out portalHit))
                 {
                     Debug.Log(portalHit.collider.gameObject);
                 }
             }
         }
     
     }
 }

I also angled the portal camera a bit to made sure i don't have "false positives" but still i can't assure that this code works on every possible angle or orientation. But it should be a good start.


hittest-through-portal.png (184.0 kB)
Comment
Add comment · Show 10 · 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 AmasterAmaster · Jul 30, 2015 at 12:35 PM 0
Share

It does help a bit, I will look into RaycastHit.normal and experiment with what you have mentioned. Because right now, I do have a main camera facing a plane that is rendering another view (like a bird's eye view/top-down view) at a location. And different objects can be in different locations but all can still be seen by this camera view. (I am not sure if orthographic or perspective view may change the way it processes the click, but I will experiment with this as well.)

avatar image AmasterAmaster · Jul 30, 2015 at 02:18 PM 0
Share

Ok, this is what I have so far:

 //Get/create the normal of the plane
 Vector3 normal = mouseHit.normal;
 Ray ray = topViewCamera.ScreenPointToRay(normal);
 RaycastHit screenHit;
 
 //Get the point of where it was clicked on the plane
 if(Physics.Raycast(ray, out screenHit))
 {
     //If that point was an item (another ray deter$$anonymous$$ed there was an item)...
     if(screenHit.collider.gameObject.name == "item")
     {
         print("hit");
     }
 }

This is already within the first Physics.Raycast that succeeded (mouse click from the screen), and when it comes to the second Physics.Raycast check (the render texture), it becomes false. What did I do wrong that resulted in this raycast to fail?

avatar image AmasterAmaster · Jul 30, 2015 at 04:03 PM 1
Share

Ok, it works well, and I will accept this as the answer. Thanks for the help and I also did not know that "textureCoord" would be so helpful on getting the points needed to find an object.

avatar image Falcon_DZ · Sep 18, 2016 at 04:13 AM 1
Share

@Nischo @AmasterAmaster The solution what you have said works perfectly when the render texture is applied a 3d game object. but do you have any solution how to get the same result when the render texture is applied to a UI element. i.e(panel).Thanks

avatar image NorthStar79 Falcon_DZ · Jul 22, 2017 at 12:21 PM 1
Share

did you find a way to get this work with UI element ?

avatar image jesse12521 NorthStar79 · Apr 23, 2018 at 02:37 AM 0
Share

I am also looking for a UI solution to make a typical $$anonymous$$imap teleport-on-click mechanic. any info would be a great help!

avatar image guneyozsan · Apr 07, 2018 at 03:42 AM 1
Share

At line 20 you can use portalCamera.ViewportPointToRay(hit.textureCoord) ins$$anonymous$$d of converting texture coords into camera coords.

Show more comments
avatar image
6

Answer by Andrey-Postelzhuk · Jun 23, 2020 at 08:39 PM

Version for Unity UI:

 public class RenderTextureRaycast : MonoBehaviour, IPointerClickHandler
 {
     [SerializeField] protected Camera UICamera;
     [SerializeField] protected RectTransform RawImageRectTrans;
     [SerializeField] protected Camera RenderToTextureCamera;
     
     public void OnPointerClick(PointerEventData eventData)
     {
         Vector2 localPoint;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(RawImageRectTrans, eventData.position, UICamera, out localPoint);
         Vector2 normalizedPoint = Rect.PointToNormalized(RawImageRectTrans.rect, localPoint);

         var renderRay = RenderToTextureCamera.ViewportPointToRay(normalizedPoint);
         if (Physics.Raycast(renderRay, out var raycastHit))
         {
             Debug.Log("Hit: " + raycastHit.collider.gameObject.name);
         }
         else
         {
             Debug.Log("No hit object");
         }
     }
 }

Comment
Add comment · Show 1 · 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 chrisjohnson67 · Aug 21, 2020 at 05:37 PM 0
Share

This worked perfectly, thanks!. You are a hero

avatar image
3

Answer by ForceMagic · Sep 19, 2020 at 02:25 AM

One notable thing that got my initial results wrong, as the Unity doc says it if you use RectTransformUtility.ScreenPointToLocalPointInRectangle.


.. For a RectTransform in a Canvas set to Screen Space - Overlay mode, the cam parameter should be null. ..


Otherwise, even if you pass your UI Camera you'll get weird results! So pass null if your in Screen Space Overlay!

Comment
Add comment · Show 2 · 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 guneyozsan · Sep 19, 2020 at 06:26 AM 0
Share

If there is a "should" case, I wonder why there is no warning in console if you assign one:(

avatar image lizzilizzi · Jun 08, 2021 at 03:48 AM 0
Share

Thank you! I think you just saved me a few hours of troubleshooting! :)

avatar image
1

Answer by DEGUEKAS · Aug 04, 2021 at 06:38 PM

UI version with collider 2d

 using UnityEngine.EventSystems;
 
     public class RenderTextureRaycast : MonoBehaviour, IPointerClickHandler
     {
 
         [SerializeField] protected Camera UICamera;
         [SerializeField] protected RectTransform RawImageRectTrans;
         [SerializeField] protected Camera RenderToTextureCamera;
 
         public void OnPointerClick(PointerEventData eventData)
         {
             Debug.Log("Click");
 
             Vector2 localPoint;
             RectTransformUtility.ScreenPointToLocalPointInRectangle(RawImageRectTrans, eventData.position, UICamera, out localPoint);
             Vector2 normalizedPoint = Rect.PointToNormalized(RawImageRectTrans.rect, localPoint);
 
             Vector2 screenPosition = RenderToTextureCamera.ViewportToWorldPoint(normalizedPoint);
             RaycastHit2D hit = Physics2D.Raycast(screenPosition, -Vector2.up);
 
             if(hit != null)
             {
                 Debug.Log("Hit: " + hit.collider.gameObject.name);
             }
         }
     }
Comment
Add comment · 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

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect which object is clicked with RayCast 1 Answer

[C#] Raycast based physics and clipping 0 Answers

Returning list of Triangles, Vertices or Points by raycasting through a mesh (iPhone) 1 Answer

Raycast Ignoring Distance 1 Answer

Physics.Raycast not checking layermask properly? 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