Search Unity

How do you use detect clicks on objects in a RenderTexture?

Discussion in 'Scripting' started by RecursiveFrog, Aug 26, 2012.

  1. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    350
    I'm running in circles trying to find the answer to a problem I would like to solve.

    I am using RenderTexture to create a screen-within-a-screen effect where CameraA renders onto a texture seen by Main Camera. I would like to be able to click objects depicted in that RenderTexture to select them, but I'm having a lot of problems trying to get the math right for my raycasting. It's not so simple as to use the Input mouse or touch coordinates directly because they mean something different to CameraA (the one that needs to generate the Ray to be cast) an Main Camera.

    The toughest part is that the Screen.width/height actually matter here. If I compensate with math for one screen size, it screws up at a larger or smaller one.

    Is there a solution for this problem that I'm just not getting?
     
  2. RecursiveFrog

    RecursiveFrog

    Joined:
    Mar 7, 2011
    Posts:
    350
    I've solved the issue. It requires you to proportionally raycast from between 0 and the dimensions of the RenderTexture (in my case 512x512).

    You first have to translate the bare screen coordinates into a position on the render texture. Once you have that, you raycast to whatever percentage of the distance width and height wise your point happens to be inside the region represented by the rendertexture and multiply that by the dimensions of the rendertexture itself.
     
  3. durvalp1

    durvalp1

    Joined:
    Aug 2, 2013
    Posts:
    3
    Hi!

    Can you please post an example of this solution? I've been trying to make a solution similar to this one but with no success. I've tried to use different types of coordinates, but there is always a difference between the render texture and elements and the coordinates where raycasthits are detected.

    Thank you in advance!