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

Move Rect Transform

Discussion in 'Immediate Mode GUI (IMGUI)' started by Mazdamundi, Dec 5, 2017.

  1. Mazdamundi

    Mazdamundi

    Joined:
    Dec 5, 2017
    Posts:
    9
    Hello everyone !

    So firstly here a little presentation:
    I am French 3D Artist (Sorry for my bad english :p) and I started ( few days ago) Unity to achieve AR. Everything is going well, I done a lot I wanted but I hang on something!

    So my goal is to make an AR on a 3D that I realized. I done the tracking on a photo (rather simple ... But I will do it on a real object after). I done display / un-display the menus according to my tracker which is visible or invisible.
    By cons I would have to do a small effect of "scanner". So for that I create a "pannel" that I move. Except that the goal is that once you get to the end of the screen the "pannel" back to the beginning and that's where I block. Here is what I managed to get so far:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Scanne_Position : MonoBehaviour {
    8.  
    9.    RectTransform myRectTransform;
    10.  
    11.        void Start()
    12.        {
    13.        myRectTransform = GetComponent<RectTransform> ();
    14.        myRectTransform.localPosition = new Vector3(-Screen.width/2,0,0);
    15.        }
    16.  
    17.        void OnGUI()
    18.    {
    19.        myRectTransform.localPosition += Vector3.right * 3;
    20.        if (myRectTransform.localPosition == new Vector3(Screen.width/2,0,0))
    21.        {
    22.            myRectTransform.localPosition = new Vector3(-Screen.width/2,0,0);
    23.        }
    24.            
    25.    }
    26.        
    27.    }
    28.  
    To specify the 0 equals the center of my screen (I do not know why) I tried to put this : (myRectTransform.localPosition > new Vector3 (Screen.width / 2,0,0)) but the > is not accepted I don't know why.
    In short, if someone has a solution I take it!

    Thank you very much !

    Maz
    http://www.quentincouvreur.com
     
  2. Mazdamundi

    Mazdamundi

    Joined:
    Dec 5, 2017
    Posts:
    9
    I think I found the solution !!

    Here the new code :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Scanne_Position : MonoBehaviour {
    8.  
    9.     Transform myTransform;
    10.     float a = Screen.width/2;
    11.  
    12.         void Start()
    13.         {
    14.         myTransform = GetComponent<RectTransform> ();
    15.         myTransform.localPosition = new Vector3(-a,0,0);
    16.         }
    17.  
    18.         void OnGUI()
    19.     {
    20.        
    21.         myTransform.localPosition += Vector3.right * 5;
    22.         if (myTransform.localPosition.x > a)
    23.         {
    24.             myTransform.localPosition = new Vector3(-a,0,0);
    25.         }
    26.            
    27.     }
    28.        
    29.     }
    30.