- Home /
Show text on colision
I need some type of sign, when if player touched trigger, the GUI text is showing, and when player is not touching it, the GUI text is gone. Here's my current script:
var ShowText : boolean = false;
var center : Vector2;
center.x = Screen.width / 2;
center.y = Screen.height / 2;
var bw = 100; //width (for posting at center)
var bh = 50; //height (for posting at center)
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.CompareTag("Player"))
{
ShowText = true;
GUIText (center.x - (bw/ 2),center.y - (bh / 2),bw,bh, GUIwillshow);
}
}
Answer by nikescar · Aug 17, 2012 at 03:23 PM
Something like this should work for you:
var colliding : boolean;
function OnGUI()
{
if (colliding)
{
GUI.BeginGroup(Rect(Screen.width/2, Screen.height/2, 100, 100));
GUI.Label(Rect(0,0,100,100), "Hit");
GUI.EndGroup();
}
}
function OnTriggerEnter(col : Collider)
{
if (col.gameObject.CompareTag("Player"))
{
colliding = true;
}
}
function OnTriggerExit(col : Collider)
{
colliding = false;
}
Or if you want to use GUIText, just set the "text" variable in the inspector to your pre-placed GUIText and use this code:
var text : GUIText;
var colliding : boolean;
function Update()
{
if (colliding)
{
text.enabled = true;
}
else
{
text.enabled = false;
}
}
function OnTriggerEnter(col : Collider)
{
if (col.gameObject.CompareTag("Player"))
{
colliding = true;
}
}
function OnTriggerExit(col : Collider)
{
colliding = false;
}
Looks good, no errors, but the GUI text is not showing. $$anonymous$$aybe I'm doing something wrong? I've attached script to the sign. I can just change in the inspector "Coliding" true and false.
I've edited the answer with an option for using GUIText if you'd rather go that route. Also, the "colliding" variable should probably have a "private" in front of it so it does show in the inspector.
Alternatively you should be able to place the text.enabled / text.disabled directly in the OnTrigger functions.
2nd script looks better, but still no results. It hides/shows the GUI text, but only when I'm changing this in the Inspector, not when I'm touching sign :/
This script should be attached to the sign. The script checks to see if the object it is attached to is colliding with an Object with the "Player" tag.
Answer by Weitzel · Aug 17, 2012 at 03:26 PM
So, a pattern I've used is to create an object (like a sphere) around the object you want to interact with, that has a collider (with trigger checked).
Separate the behaviour so it can be independant of the dialogue. You can later abstract the dialogue to multiple types easily if you need to. The collision object would have a script attached only has one concern:
var signText : String;
private var dialogue;
function Update () {
}
function OnTriggerEnter(other : Collider) {
if(other.gameObject.GetComponent(playerTag) != null)
startDialogue();
}
function OnTriggerExit(other : Collider) {
if(other.gameObject.GetComponent(playerTag) != null)
stopDialogue();
}
function startDialogue() {
dialogue = gameObject.AddComponent(simpleDialogueObject);
dialogue.setTextToDisplay(signText);
}
function stopDialogue() {
Destroy(dialogue);
}
where your "simpleDialogueObject" would looke something like:
private var signText = "";
function OnGUI () {
GUI.Box (Rect (Screen.width * 0.05 ,Screen.height * 0.5, Screen.width * 0.9 , Screen.height * 0.45), signText);
}
function setText(newText : String){
signText = newText;
}
Hmm. It's showing me 3x $$anonymous$$ identifier. Trigger is on, by the way.
Gah, it's hard to be newbie.
So this is pseudo code, and the unknown identifier is probably because its doing checks against class names that are different from what you may or may not have defined. The more pointed your question, the more I can help.
It is hard being a noob, stick with it and keep asking very useful, helpful, and pointed questions like these, they help.
What can I else say? Player is named First Person Controller (how original), sign is named sign, text should appear on the center, JS would be nice... That's all.
Thanks for help anyway, I'll try some different ways.
when you say something like
gameObject.AddComponent(simpleDialogueObject);
it will try to add a script named "simpleDialogueObject" to the gameObject you are talking about. if you don't have a scripted named this, it will claim it is an "unkown identifier" when it tries to compile. even if capitalization is off.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trigger Script Not Firing 4 Answers
Need help making an audio trigger. 2 Answers
How to trigger a Script from an Object. 0 Answers