Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotating Rect Transform

Discussion in 'Scripting' started by unitynoob24, Sep 9, 2015.

  1. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Hey guys!

    I am looking to rotate an image on my canvas via its Rect Transform value. I have tried this on all of the different Canvas types and it still does not rotate. Your thoughts? Thanks guys!

    Code (csharp):
    1.  
    2.  
    3. var timeCounter : int = 0;
    4. var x : float;
    5. var y : float;
    6. var z : float;
    7.  
    8. function Update()
    9. {
    10. timeCounter += Time.deltaTime;
    11.  
    12. x = Mathf.Cos(timeCounter);
    13. y = Mathf.Sin(timeCounter);
    14. z = 0;
    15.  
    16. GetComponent.<RectTransform>().tranform.rotation = Quaternion.Euler(x,y,z);
    17.  
    18. print("X: " + x + "Y: " + y + "Z: " + z);
    19. }
    20.  
    21.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    transform
    not
    tranform


    also I believe you don't need the GetComponent.<RectTransform>(), you should just be able to reference the transform directly
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    As a note, this code shouldn't compile, which means you should have been able to see an error in the Unity console (check the very bottom of the window). This is your first place to look for problems, and when you come here, always paste in any errors you have along with the code!
     
  4. lib87

    lib87

    Joined:
    Aug 28, 2015
    Posts:
    17
    You can press F5 in the monodevelop to see if the code compiles... Also to rotate (simple) you can use this method

    this.transform.Rotate(Vector3.right, Mathf.Deg2Rad * rotationY); // This is for right left rotation which im using with mouse drag... if you change Vector3.right to Vector3.up then you will have upwards rotation too
     
  5. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Hey guys sorry for the delay I do have it in Unity as Transform, that was a typo when I wrote it here in the thread!

    I will try what you suggested Lib!

    Thanks guys!