This may be strange, but I want to interrupt first-person gameplay every 20 seconds by cutting to a string of text on black background, and then cutting back to gameplay after a few seconds. Kind of like the intertitles in a silent film.
I want it to randomly pick from a set of lines I’m writing, and I’d also like to associate each text line with a short audio sample that will play when it’s displayed.
I’ve very little experience coding, and am not really sure how to go about this. I did try to create and position a GUIText across the middle center of the screen, overlaying some text on the first-person camera. I’m not sure how/if this can be scripted to randomly change the text and also play a sound, though.
If anyone can enlighten or point me in the right direction, I’d hugely appreciate it!! Thank you.
Note that it’s fairly complicated to pause the game while something else, like showing a tielcard, depends on letting time pass, so you’ll have to find a way to achieve this yourself, here is the code to show your title cards periodically:
#pragma strict
import System.Collections.Generic;
var IsShowingTitleCard : boolean;//dont touch :)
var Timer : float;//dont touch :)
var TimerThreshold : float = 5.0;//set this to whatever number of seconds you want between title cards
var TitleCardPause : float = 5.0;//set this to however long you want a title card to be shown, in seconds
var TitleCardStyle : GUIStyle;//in the inspector, under the NORMAL register of this Style, for BACKGROUND select a black image (any small black image), and as TEXT COLOR choose white.
//also pick a FONT of your liking (you can copy any font into your project's asset folder), set a FONT SIZE, and set the ALIGNMENT to Middle Center (or whatever you like)
//also, check WORD WRAP.
var TextList : List.<String>;//in the inspector, type into this List's COUNT how many different Text strigns you want to have, and then type them in in the appearing fields.
//however, it's easier to do this directly in this script, see the Start() function below!
var AudioList : List.<AudioClip>;//do as above, but remember that this List's COUNT has to be the same as the total count (including things you add in Start()) of the TextList!
//anyway, drag your Audioclips into this list so they are in the same order as the text strings they correspond to.
var RandomNumber : int;
function Start()
{
//instead of typing in text in the tiny inspecot fields, just type it in these code lines. Add as many as you want!
TextList.Add("This is a title card text!");//adds another String at the end of your Text List
TextList.Add("This another title card text!");
//... And so on (delete this line)
}
function Update()
{
if(IsShowingTitleCard == false)//if the title card is not shown, increase the timer
{
Timer += Time.deltaTime;
}
if(Timer > TimerThreshold)//if the timer reaches the threshold, let the game know that a title card should be shown
{
RandomNumber = Random.Range(Mathf.Round(0.0), Mathf.Round(TextList.Count - 1));//choose a new number at random, this serves as the element position inside our Text and Sound lists.
PlayTitleCardSound();//call a function that plays the righht audio file. This GameObject needs an audio source, and the scene has to have an object that has an audio listener!
IsShowingTitleCard = true;
Timer = 0.0;//also, set the timer to 0.00 as long as the title card should be shown
}
}
function OnGUI()
{
if(IsShowingTitleCard == true)//if the title card should be shown...
{
ShowTitleCard();//call a seperate function that actually shows our title card.
//by using a seperate function, we can include the yield WaitforSeconds line which wouldn't work if used directly in OnGUI()
}
}
function ShowTitleCard()
{
GUI.Label (Rect (0, 0, Screen.width, Screen.height), TextList[RandomNumber], TitleCardStyle);//display our text at the specified xy coordinates, in the specified xy dimensions, with the specified style
yield WaitForSeconds(TitleCardPause);//Wait the number of seconds we specified...
IsShowingTitleCard = false;//and then let the program know that we don't want it to show our title card anymore.
}
function PlayTitleCardSound()
{
audio.clip = AudioList[RandomNumber];
audio.Play();
}