- Home /
Intro, Text scrolling wrong direction (Down not Up)
I found this example it works, i modified it a bit for a skip feature and aligned it center. All works fine but it scrolls from the Top to the Bottom? I want the other way around > Bottom(start) to Top(stop) well it fades out but you know what i mean..
Is there a quick fix i can add to reverse it?
public var intro : String[];
public var off : float;
public var speed = 12;
function Start (){
audio.Play();
}
function OnGUI()
{
if(Input.GetKeyDown("space")){
print("Space Pressed");
yield WaitForSeconds (2);
Application.LoadLevel (0); //Test Main Menu
}
var Skip = new Rect((Screen.width/2)-10,(Screen.height/1)-30, 250, 80);
GUI.Label(Skip, "Press (Space) To Skip Intro");
off += Time.deltaTime * speed;
for (var i = 0; i < intro.Length; i++)
{
var roff = (intro.Length*-20) + (i*20 + off);
var alph = Mathf.Sin((roff/Screen.height/1)*180*Mathf.Deg2Rad);
GUI.color = new Color(1,1,1, alph);
GUI.Label(new Rect(650,roff,(Screen.width/2)-50, 30),intro[i]);
GUI.color = new Color(1,1,1,1);
}
}
(You can't use coroutines inside OnGUI - it will generate a compile error)
Answer by Statement · Dec 22, 2013 at 12:18 AM
There are two things you need to do:
Reverse the direction of the texts motion
Start the text at the bottom of the screen
You should be able to set Speed to a negative number via the Inspector to make text flow upward (but this alone will just render your entire view blank since it keeps scrolling up from the top of the screen).
You should also be able to set Off to a high enough positive number via the Inspector (or via Start
) so the text start at the bottom of the screen. If you want to support multiple resolutions, check out Screen.height and base your off
variable from that.
Thanks for the reply, i just tried the
public var speed = -12; in the script. Then i removed that and added the -12 in the inspector but noting appears.
$$anonymous$$aybe it's off the screen now.
off -= Time.deltaTime * speed;
$$anonymous$$akes it disappear completely.
Ok, just changing the default value in the code to -12 won't work and I told you to change it via the Inspector (for a good reason) :)
The reason is that Unity have stored the value 12 since before (or whatever value you set it to via the Inspector). The default value is only used when resetting a component or when adding a new component. Select your object in the heirarchy, and modify the value using the Editor.
Secondly, you shouldn't have to change the direction in that other snippet of code. Undo your changes.
Thirdly, you are still required to set off either via the Inspector, or adjusting it at Start to match Screen.height.
Ex:
Set Speed -12 via the Inspector
Undo your change to the second line you commented on
Set Off to 400 via the Inspector
Depending on your resolution, you are likely to see the text start climbing up from somewhere midscreen. If you increase Off, it will start further and further down.
Yeah i fixed that -12 via inspector, sorry i miss read it. Im slow tonight it's working perfect now thanks man.
In the Inspector Off just appears as a 0
Yes. So set it to a value like 400. (400 pixels down from top of screen). If text starts in middle, increase it to 800. But this is a brittle approach. If you set off = Screen.height;
inside Start chances are that it would match up better.
Answer by RyanZimmerman87 · Dec 22, 2013 at 01:05 AM
Here is an example how I do scrolling text, the downside of this method is that it is frame rate dependent. OnGUI() is called at least twice per Update(). But I guess depending on the functionality or appearance you are going for that won't be a big deal.
I personally use a matrix to format to any aspect ratio, but this may not be the best approach to scale textures correctly. There has been some helpful answers to my post that make it seem like it could be possible to do it with the GUI.matrix, but you are probably better off using individual GUI Textures on empty game objects with a scaling script from:
Components > Rendering > GUI Texture
Seems excessive to do this for every single GUI component if you have a ton like I do. Hard to say what's the best way to do this many suggest not even using OnGUI() and getting some 3rd party products from the Asset store such as 2D Toolkit.
I know some of this information isn't related to your specific question I just kind of want to give others some info and a early warning if they are getting started with OnGUI(). I put in a ton of work into mine, and I'm not sure if I agree with using the GUI.matrix as the ultimate solution, it allows easy size and position for any screen, but not really that great with scaling it seems.
There are a few people who posted some useful answers so it may be possible to do it with the GUI.matrix. I have yet to try this one for example that looks like it could possibly work if you determined to go GUI.matrix:
http://entitycrisis.blogspot.gr/2010/11/automagic-gui-scaling-in-unity3d.html
Also scaling solutions like AspectRatioEnforcer seem like they would be good if you use GUI.Textures on individual game objects with the AspectRatioEnforcer script.
K anyways now that I gave my little speech since this stuff can be quite messy and people should be very careful before they commit to something if there could be better solutions for them.
Here's an example of how I have GUI text float up the screen in my game, would work for labels or textures as well:
//easily edit GUI components
public GUIStyle potionStyle;
int GUIDepthInt = -1;
Vector3 matrixVector;
float native_width = 1280;
float native_height = 720;
float rx;
float ry;
int textUpCounter;
void Start()
{
//scale for your matrix
//this is where things get messy at different aspect ratios for your scale
//if you always use a specific native width and height like 1280, 720
rx = Screen.width/native_width;
ry = Screen.height/native_height;
matrixVector = new Vector3 (rx, ry, 1);
potionStyle.normal.textColor = Color.red;
textUpCounter = 0;
}
void OnGUI()
{
if (GUIButtonsScript.gamePausedBool == true)
{
return;
}
else if (potionObtainedBool == true)
{
GUI.depth = GUIDepthInt;
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, matrixVector);
++textUpCounter;
//this will center your text if you use the GUIStyle with "Upper Center" allignment
//with the 1280, 720 formatting matrix
GUI.Label (new Rect (640, 140 - textUpCounter, 0, 0), "+1 Health Potions!", potionStyle);
}
}
So in this example you switch the healthPotionBool to true when you pick it up, it shows the text scroll up in center of screen.
I personally start a coroutine that destroys the health potion at the time the text should be disappearing off the screen.
I also enable this script when the player becomes fairly close to the potion.
Keep in mind that OnGUI() is VERY performance heavy calling twice per update, so if you are going to be using it on individual objects you will want to disable those scripts when out of range, and disable them again when the text display is not being used or just destroy the Game Object entirely.
Unitys GUI is performance heavy but perhaps not so because of calling OnGUI twice (or even more than that) per frame, but the rendering is not batched so GUI elements are one draw call each.
Ok thanks for the post, i got $$anonymous$$e working now, but i will defiantly take a look at that later, im doing a lot of different stuff trying to learn/figure it all out.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124637im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RTS Style Camera Scrolling 8 Answers
Who Killed Who - Scrolling GUI Text 2 Answers
Texture wont scroll on y-axis. 0 Answers