Search Unity

Render a camera to a cube

Discussion in 'Scripting' started by PMTheQuick, Jul 14, 2009.

  1. PMTheQuick

    PMTheQuick

    Joined:
    Jul 13, 2009
    Posts:
    5
    Hi

    I will show a camera on a normally cube. I searched here and found RenderTexture... My actual code:

    Code (csharp):
    1. var cam : Camera;
    2.  
    3. function Update () {
    4.     cam.targetTexture = new RenderTexture(128, 128, 32);
    5.     cam.targetTexture.isPowerOfTwo = true;
    6.     cam.targetTexture.Create();
    7. }
    Now i have thinked i add "var cube : Transform;" to specify the cube. This works. But how i can make, that the cube have the RenderTexture as Texture?

    Camera in Unity -> Render to cube texture

    How i can make this? Thank you!

    Greetings
    PMTheQuick
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You need to assign your render texture to the material your cube is using. Get a reference to that cube game object and do something like this:

    Code (csharp):
    1. cubeGO.renderer.material.mainTexture = cam.targetTexture;
    Also, Update is not the best place for your code as that is executed every frame and you surely don't want to recreate your render texture anew each and every frame. Move that code to Start for example and you'll be much better off.
     
  3. PMTheQuick

    PMTheQuick

    Joined:
    Jul 13, 2009
    Posts:
    5
    Thank you very much! I updated my code and.... it works! :D

    Greetings
    PMTheQuick