Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

rotate object with color of a texture

Discussion in 'Scripting' started by sno_on, Jul 5, 2015.

  1. sno_on

    sno_on

    Joined:
    Nov 15, 2013
    Posts:
    3
    hi, i try to write a script for rotating an object in a specific angle
    - the values for this angle should come from a black & white movie texture -

    for example: the color 1,1,1, 255 should rotate the object just on x axis with an angle of 1,4 degree.
    the color 2,2,2, 255 rotate the objects to 2,8 degrees .... and so on - till every of the 255 colors have a specific angle.

    with this script its possible to read the color and rotate one object...
    maybe someone has a useful tip for me - because iam totaly stuck at this point

    thanks
    Code (CSharp):
    1.  
    2. public RenderTexture myRenderTexture;
    3. Texture2D tex;
    4. public Color col;
    5. public Color colorMin = new Color(0F,0F,0F,255F);
    6. public Color colorMax = new Color (255F,255F,255F,255F);
    7. public GameObject cube1;
    8.  
    9.  
    10. // Use this for initialization
    11. void Start () {
    12. tex= new Texture2D(myRenderTexture.width, myRenderTexture.height);
    13.  
    14.  
    15. }
    16.  
    17. // Update is called once per frame
    18. void Update () {
    19. RenderTexture.active=myRenderTexture;
    20. tex.ReadPixels(new Rect(0, 0, myRenderTexture.width, myRenderTexture.height), 0, 0);
    21. tex.Apply();
    22. col=tex.GetPixel(myRenderTexture.width/2, myRenderTexture.height/2);
    23.  
    24.  
    25. if (col == colorMin || col == colorMax)
    26. {
    27. //col = new Color(255.0F,255.0F,255.0F,255.0F);
    28. cube1.transform.Rotate(0, 0, rot*Time.deltaTime);
    29.  
    30.  
    31. }
    32.  
    33.  
    34. }
    35. }
     
    Last edited: Jul 7, 2015