- Home /
How convert the coorardinates of the mouse from one camera to another?
Let's say I have 2 cameras one looking at a mesh with a render texture and another camera setup to the render texture. How would I convert the position clicked on the mesh with the render texture to the position of the second camera? I have a picture to make my question clearer.
Answer by Remy_Unity · Jan 16, 2018 at 01:16 PM
That's a bit of a tricky question :
Imagine that your mesh with the render texture is a kind of portal to your second camera. What you need to do is to trace a ray from the first camera to to mesh, "transfer" this ray to the second camera, an continue it from there to the "real" mesh.
The second camera near plane corresponds to the mesh with the render texture.
Right, i first missed the part with the RenderTexture ^^. So i thought alankemps answer would fit. However you are actually more on the point.
Since the rendertexture is a flat image the inco$$anonymous$$g angle of the ray actually doesn't matter. So all you need to do is find the point on the rendertexture where your first ray hits. If the rendertexture mesh uses a meshcollider you get the textureCoord for free. This can be used in Camera.ViewportPointToRay on the rendertexture camera to cast a second ray into the actual scene.
Answer by alankemp · Jan 16, 2018 at 10:01 AM
You can use Camera.WorldToScreenPoint to convert the world position (where you clicked on the mesh) into the screen position on the second camera.
Your answer
Follow this Question
Related Questions
Rendertexture raycast 0 Answers
Question about sending physics raycast from the camera 1 Answer
Camera/mouse movement issues "almost but no dice" 1 Answer
Raycast an Object to the mouse position 1 Answer
Raycast Object Selection 3 Answers