- Home /
Simple text in a box - on player collision
As a designer more then a programmer im currently lost in the mist. I know how triggering works but dont seem to get how this one should work :
1) SITUATION: A player (tagname) collides with an object. ----> i already know how to make a simple check if or not player touches object...
2) Object has a collision script on it showing a text. ??????? How do i tell the collision script to look for a tekst?
3) Object shows a tekst in a box middle of the screen. ??????? How to position the text?
3) Box & text go away when user press enter. ??????? How to tell unity when a button is entered?
It would be great to have a variable tekst that van be changed in inspector. This will allow me to recycle the same script every time i need to show a tekst.
Im really worthless in scripting and trying to avoid it since this is a easy "myst like" game. But now I need a full script in C or java. Normally i figure these things out myself, but this one is cracking my head for days now.
Anyone that would be willing to help me out with a full functional script would have my sincere thanks !!!
Answer by konashadow · Dec 06, 2012 at 03:40 PM
Once you check the collision with onCollisionEnter or whatever you want, you can toggle some boolean variable (lets use "collide") to true.
Then you can use a GUI label to make the text appear.
if(collide) {
GUI.Label(new Rect(10,10,50,50), "Collide!");
if(Input.GetMouseButtonDown(KeyCode.Return)) {
collide = false;
}
}
This code will make a text saying Collide! when collide is set to true. You can edit the position by editting the first two numbers in the 4 number sequence. The last two edit the size. The GetButtonDown will sense when you hit enter, and will set collide to false, making the text disappear. You can put this code in the update function. Also, you could declare a public string variable and replace the "Collide!" with it so you can edit the message of every collision. Hopes this helps!
Answer by Xerelin · Dec 06, 2012 at 03:45 PM
Inside your collision event you need to set the collider you hit as a parameter, then inside the event call up the value of the public variable housing your text.
Next, store this text in a string value at the class level and switch a bool from false to true for your GUI.
Lastly, in your OnGUI() method, you need to check to see if the bool is set to true and if it is, draw a text area on the screen. Sample code is below:
float halfwidth = 0f;
float halfheight = 0f;
bool isCollision = false;
string textToShow = "";
void OnCollisionEnter(Collider hit)
{
textToShow = hit.textToDisplay;
isCollision = true;
}
void OnGUI()
{
if(isCollision){GUI.TextArea(new Rect((Screen.width / 2) - halfwidth, (Screen.height / 2) - halfheight, (halfwidth * 2), (halfheight * 2)), textToShow);
}
void Update()
{
if(Input.GetKey(KeyCode.Enter)){isCollision = false; }
}
Inside the script on your object being collided with just store a string value with what you want to display at the class level.