I would like to have an NPC talk to the player (eg, over a radio or telepathically) while they’re playing, and have subtitles for what they’re saying (perhaps on top of the screen?) when they do. I want these to be triggered by say, walking into specific parts of the level, or picking up collectibles. How do I implement this sort of feature in Unity? I’ve read something about box colliders as children of the player, but that seems like a lot of trouble for a game with a lot of dialogue like the one I’m working on.
Any help is appreciated! For the record, I’m pretty new to Unity, but I’m fine with scripting (provided I can figure out what kind of methods I’m dealing with).
I would suggest simply implementing a trigger collider for each section of the game in which you want to initiate dialogue. You can do this by simply setting up an empty GameObject with a Box Collider with IsTrigger set to true.
For your situation, in order to allow for the correct dialogue to appear on-screen relative to the point in game, on each individual trigger object, add this script:
var dialogue : String[];
var output : GUIText;
private var curLine : int = 0;
function OnTriggerEnter (collider : Collider) {
if (collider.CompareTag("Player")) {
output.enabled = true;
output.text = dialogue[0];
}
}
function OnGUI () {
if (output.enabled && Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return) { //I used the [Return] key here but you can choose whatever key you want
curLine++;
if (curLine < dialogue.Length) {
output.text = dialogue[curLine];
} else {
curLine = 0;
output.enabled = false;
}
}
}
You will need to add a GUIText object (you should only need one for all of your trigger objects) and all of your individual lines of dialogue for that specific trigger to the Dialogue array in the Inspector.
If you have any questions, just ask
Hope that helps,
Klep
Edit:
Oh, and also I forgot to mention that you must tag your player as “Player”, so that the triggers can pick which colliders to respond to.