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

Rotate RectTransform/UIImage per script?

Discussion in 'UGUI & TextMesh Pro' started by Olipool, Feb 13, 2015.

  1. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Hi there,

    i wanted to build an old fashioned watch (with moving hands) in my GUI. I put the background image on a canvas and then also two UIImages for the two hands. I positioned them and pivoted them to the center of the background image (the actual clock image) and my plan was to rotate these images according to the time. Unfortunately there seems no way to access the rotation of the rectTransform from the script? I tried to access the rotation directly on the rectTransform, I tried to get a transform and I searched a while but found no solution.
    I don't want to make 60 single images and swap them every minute and I don't want to do an animation and control that by code (if even possible).
    So please can you give me some advice on this?

    Thanks a bunch!

    edit: since RectTransform inherits from Transform, why can't I access the transform.raotation...?
     
    Last edited: Feb 13, 2015
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Gnaa silly silly me...after working too long on one thing you don't see the simplest mistake...I tried to set the rotation OUTSIDE of a method...:rolleyes:
    So bottom line: RectTransform.rotation works just fine...
     
    SimonDarksideJ likes this.
  3. magonicolas

    magonicolas

    Joined:
    Dec 4, 2013
    Posts:
    30
    I need to do this, But Im not sure how you solve it, can you please give me more detail on the c sharp code of how to do it?
    Thanks!
     
  4. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Of course, here is my script for the clock.
    My GameManager script has s static variable named clock which is increased in every update. Of course you could maks a clock variable inside the clock script.
    In the inspector you have to drag a text field and the two UIImages for the both hands in the public fields of the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Clock : MonoBehaviour {
    6.  
    7.     public Text display;
    8.     public RectTransform littleHand;
    9.     public RectTransform bigHand;
    10.    
    11.     Transform test;
    12.    
    13.     int hours;
    14.     int minutes;
    15.     string paddingChar="0";
    16.     // Use this for initialization
    17.     void Start () {
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.       hours=Mathf.RoundToInt(GameManager.clock/60f);
    23.       minutes=Mathf.RoundToInt(GameManager.clock%60);
    24.       if (minutes<10){
    25.           paddingChar="0";
    26.       } else {
    27.           paddingChar="";
    28.       }
    29.           display.text=hours.ToString ()+":"+paddingChar+minutes.ToString();
    30.           littleHand.rotation=Quaternion.Euler(0,0,-hours*360/12);
    31.           bigHand.rotation=Quaternion.Euler(0,0,-minutes*360/60);
    32.     }
    33. }
    34.  
     
    songsongsongsong likes this.
  5. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    What is the purpose of line 11. Transform test; ?
     
  6. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Oh sorry, that has no purpose. It was left over from one of my previous tests :)
     
  7. Sixbraker

    Sixbraker

    Joined:
    May 20, 2017
    Posts:
    8
    you're so cute