Using Unity Script to Create an NPC Talk System
In this third part of our robot unity tutorial series we’ll be creating a talk system that allows NPC characters such as our Holobot to talk to us by displaying text on the screen through a GUIText object. The player can then iterate through this text using a key defined in the Input Settings.
We’ll start off with a static variant where the lines of text just appear and disappear. We’ll then build on top of that to create a “scroll-in” effect for the text, similar to how some of the older games did it.



Is this Citizen only? Doesn’t seem to be a video attached…
Apologies for that; something went wrong in the publishing of this post. It’s fixed now!
Thanks! All good now!
thank you so much for these! Ive been lerning a lot, one question though, are you going to be able to do a tutorial on controling the camera? (like the lego games style?)
I don’t think I’ve ever played any of the lego games; could you explain a bit on what you mean?
the camera is at a high angle, and tracks on to the player (it doesnt move much)
Ah, I see what you mean (sorry, I couldn’t youtube it this morning as I was on my phone). I might do a tutorial on this some time in the future, though I can’t give you an estimate on when that will be. For now, I’d suggest looking into the Standard Assets’ “Smooth Follow” script, and also maybe the Transform.LookAt() function (http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html).
Good luck and thanks for the suggestion!
-Patrick
Cool tut. Question for you Patrick. Can you think of any Learning JavaScript books (beginner) that I could get that would help me follow the scripting tuts a little better? I’d like to have a better JS knowledge basis before going too crazy building my own games. I can search Amazon all day looking for a good book, but would rather ask someone like you, who I know knows what he is talking about.
Hi Stacey,
Most books on Javascript will talk about the real Javascript, the web implementation. Unity on the other hand uses Unityscript, which has a syntax variant very similar to JS. However, other than the syntax they’re completely different.
I’d suggest looking through the API to get a feel of how methods and properties are grouped, then try to make a simple game. Don’t worry about having to look up everything if that happens, that’s very normal when you first start out.
If you’re looking for some more general information like programming definitions, styles and paradigms, I’d suggest looking for either general, non-language-specific info on that, or based on C#. The ‘normal’ implementation of C# is pretty much (if not completely) identical to Unity’s, so the gap to bridge there is much smaller. C# is also a language that’s not too different from Javascript, so it’s fairly easy to transfer over the knowledge.
Hope this helped and let me know if you need further help!
-Patrick
An other time, very great tutorial! I realy needed a tutorial about NPC talking. Only a question… is it possible that in the future you will show a tutorial about AI Artificial Intelligence? Thank you for all.
Hey MartÃ,
I actually already have some code for that in my local sandbox project.
We currently have it planned as a Citizen Exclusive tutorial, which I’m guessing right now would go live either March or April.
-Patrick
WOW!! Thanks for the reply. This is brilliant becouse the AI is one of the most difficult topic in scripting I think.
I’m waiting to March!
Sincerely, a teenager of Catalonia.
Glad to hear you’re so excited about it! I’ll try my best to have it live up to your expectations.
How can I do something as: NPC Talk: “What do you want?”; So the player can choose between 2 options, for example, “Cube” or “Capsule”; If choose cube, a cube appears in the terrain, if the capsule, a capsule?
I really like your tutorials
Thanks!
Hey Wendel,
Nice idea of making it interactive! There are, as with most things in programming, many ways of going about this. I’ve created a little stub in which I assume you’ve placed several pre-made, filled in GUITexts containing the options, as well as a ‘choice cursor’ in your scene. Please note it’s just some quick psuedo-code, but will hopefully give you an idea of how it could be done. (Also, there are many ways to make this approach better, for instance by only making a String[] for the options, then dynamically creating the GUITexts out of that. This is really just a stub kind of thing.)
The stub code: http://pastebin.com/kaZJvXwR
Hope this helped and good luck,
Patrick
I will try that! Thank you!
Hi Patrick! Thanks for this great tutorial! I have a question regarding the guiLayer here. Can i use a guitexture beneath the text and what would be the best way to implement this?
Hey Dreamcreator,
Yes, you can! The z-position determines a GUIElement’s depth in screen-space.
-Patrick
I dont know if you know this, but the holo bot head has the texture for the body.
Hey John,
You mean the particles’ texture? I did that on purpose to make it seem a bit more glitchy. =)
-Patrick
I mean the actual texture its self, on the head
Is there a way that I can only have the NCP talk if I press a button over his head. (If so how can I add a textured button?)
It’s almost never a question of “can I”, but rather “how can I best approach this”.
It’s definitely possible; I’d recommend taking a look at Unity’s GUI (the OnGUI() function) and maybe the GUITexture component.
-Patrick
Thanks! Also what is the best approach to adding a textured background to text, as it’s almost invisible in a snowy area.
If you’re using the OnGUI system I think the easiest way would be to add a GUI.Box(). Otherwise I’d suggest looking around on the Unity forums; they’re a very nice and helpful community. =)
Could you not have passed the line to be scrolled as a parameter to startScrolling()? Something along the lines of…
function startScrolling(scrollText:String){
//just reference scrollText here, it will save processor time
}
Using this would (I think) be slightly faster as it only has to refer to one variable, rather than first getting the value of currentLine, then getting the value at that index in talkText. It would only be a small increase, but hey, it’s slightly more efficient. I think.
Addendum to previous post:
Yes, it would require that those two variables were accessed, but wouldn’t need it to be done as many times. As it stands at the moment, they need to be accessed for each pass of the FOR loop. Also, I haven’t finished watching the video, so you may have done this and I haven’t seen it yet.
Yup, you’re absolutely right; nice catch! =)
Thanks for these tutorials! You’re a great teacher, and you show that through your mistakes(and fixing them swiftly).
Hi, thanks so much; glad you’re enjoying these tutorials! =)
-Patrick
Love the tutorials, very clear and i can understand them. When will the shooting tutorial be coming online? Anyway keep up the good work, very impressed in the time you take to explain stuff.
Cybershead
Hi Cybershead,
Thanks for the kind words! I’ve been a bit preoccupied with CGCookie’s in-house project Eat Sheep and some medical issues, but I’ll try and have it up before the end of May.
Cheers,
Patrick
I look forward to that then, hope the medical issue was sorted out, keep say and sane with eating sheep.
Cybershead
Hi Patrick,
I have hit a dilemma, My code background is Action script and i was no master at that. I see a lot off tutorials in JS and a lot of others in c#. I do feel more comfortable with JS but i see a lot of people saying that c# is a lot better to make games with, so as you are a guru at both, what do you think i should learn?
Cybershead
As a programmer with some experience in a few languages, by no means an “expert” but enough to give advice anyway, I suggest you start learning C#.
It isn’t really as difficult as it looks. It can be a little fussy about some syntax things, however, picking up Javascript (“Unityscript” technically) is a lot easier once you understand a little of C#.
Imagine learning the formal Legalese writing style…(though not as complicated) it can be used in certain situations that casual writing can’t… like court orders and such… then when you switch back to casual writing, it feels a lot easier because there’s less rules.
That’s sort of how C# versus Unity-JS is. There are some things Unity-JS can’t do that C# can, which is likely why you’ve been pushed toward it…
That said, Unity-JS is fine for -most- scripts you’ll write. If all you plan on doing is moving things around and changing animations and colors, you should be fine with JS… but when you eventually find a reason to need C#, it’ll be a little tougher to switch.
In short, C# gives you a lot more control, while JS gives you a bit more visual fluidity… IE it’s a little easier to read ad write it, but not quite as versatile.
Hope that helps, and I’d like to see what Patrick has to say as well, when he gets to it.
do I have to use the pro version?
No, you don’t; I simply forgot to switch the skin back after working in it. =) Sorry about the confusion!
Oh, don’t worry, It’s okay!
But, how can I change my skin?
This is only possible with Unity Pro; it let’s you choose between a dark and regular skin.
But wicht of those two versions are you using?
I was accidentally still using the Pro (dark) skin in this video; usually I switch back to the regular one to avoid confusion. =) My personal favourite is dark though.
But can I still do the tutorial with the free one?
Yes, you can. If a tutorial requires Unity Pro we’ll be sure to put it in the title/ first line of the description. =)
Thanks, sorry for bothering you
More Please! Fun little series! This is bringing back to my memory all the C coding and LPCMUD coding (from those old text based RPG games). My brain is getting excited!
Hello Patrick,
Nice tutorial, one question though. Because you are updating the scroll within the update and then you have a loop inside that with a yield (all within one update call), would this cause the rest of your scene to wait for the text to scroll before moving to next frame?