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.

Unity Script: NPC Talk System

Robot Series<

Discussion

47 Responses to “Robot Series: Using Unity Script to Create a NPC Talk System”
  1. Medoway
    Posts: 2

    Is this Citizen only? Doesn’t seem to be a video attached…

    #
    1
    Feb 20, 2012 at 7:39 am
    • Posts: 109

      Apologies for that; something went wrong in the publishing of this post. It’s fixed now!

      #
      1.1
      Feb 20, 2012 at 7:48 am
      • Medoway
        Posts: 2

        Thanks! All good now!

        #
        Feb 21, 2012 at 8:22 am
  2. Posts: 11

    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?)

    #
    2
    Feb 20, 2012 at 2:57 pm
    • Posts: 109

      I don’t think I’ve ever played any of the lego games; could you explain a bit on what you mean?

      #
      2.1
      Feb 20, 2012 at 6:47 pm
      • Posts: 11

        the camera is at a high angle, and tracks on to the player (it doesnt move much)

        #
        Feb 21, 2012 at 5:53 pm
      • Posts: 109

        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

        #
        Feb 21, 2012 at 6:07 pm
  3. Posts: 1

    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.

    #
    3
    Feb 22, 2012 at 9:28 pm
    • Posts: 109

      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

      #
      3.1
      Feb 23, 2012 at 6:36 am
  4. Posts: 5

    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.

    #
    4
    Feb 23, 2012 at 2:27 pm
    • Posts: 109

      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

      #
      4.1
      Feb 24, 2012 at 10:40 am
      • Posts: 5

        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.

        #
        Feb 25, 2012 at 3:01 am
      • Posts: 109

        Glad to hear you’re so excited about it! I’ll try my best to have it live up to your expectations. ;)

        #
        Feb 25, 2012 at 4:26 pm
  5. Wendel
    Posts: 3

    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 :D Thanks!

    #
    5
    Feb 28, 2012 at 9:27 am
    • Posts: 109

      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

      #
      5.1
      Feb 28, 2012 at 5:16 pm
      • Wendel
        Posts: 3

        I will try that! Thank you! :D

        #
        Feb 28, 2012 at 10:15 pm
  6. Posts: 2

    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?

    #
    6
    Mar 1, 2012 at 5:19 am
    • Posts: 109

      Hey Dreamcreator,

      Yes, you can! The z-position determines a GUIElement’s depth in screen-space.

      -Patrick

      #
      6.1
      Mar 2, 2012 at 7:17 am
  7. Posts: 11

    I dont know if you know this, but the holo bot head has the texture for the body. :)

    #
    7
    Mar 1, 2012 at 7:46 pm
    • Posts: 109

      Hey John,

      You mean the particles’ texture? I did that on purpose to make it seem a bit more glitchy. =)

      -Patrick

      #
      7.1
      Mar 2, 2012 at 7:16 am
      • Posts: 11

        I mean the actual texture its self, on the head :)

        #
        Mar 5, 2012 at 11:27 am
  8. Posts: 13

    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?)

    #
    8
    Mar 25, 2012 at 6:54 pm
    • Posts: 109

      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

      #
      8.1
      Mar 25, 2012 at 7:39 pm
      • Posts: 13

        Thanks! Also what is the best approach to adding a textured background to text, as it’s almost invisible in a snowy area.

        #
        Mar 26, 2012 at 3:17 pm
      • Posts: 109

        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. =)

        #
        Mar 26, 2012 at 3:47 pm
  9. Posts: 9

    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.

    #
    9
    Apr 2, 2012 at 6:00 am
    • Posts: 9

      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.

      #
      9.1
      Apr 2, 2012 at 6:02 am
      • Posts: 109

        Yup, you’re absolutely right; nice catch! =)

        #
        Apr 2, 2012 at 7:55 am
  10. Adam Dalton
    Posts: 2

    Thanks for these tutorials! You’re a great teacher, and you show that through your mistakes(and fixing them swiftly).

    #
    10
    Apr 9, 2012 at 1:35 am
  11. Posts: 3

    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

    #
    11
    Apr 29, 2012 at 2:08 pm
    • Posts: 109

      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

      #
      11.1
      Apr 29, 2012 at 3:00 pm
      • Posts: 3

        I look forward to that then, hope the medical issue was sorted out, keep say and sane with eating sheep.

        Cybershead

        #
        May 1, 2012 at 2:50 pm
  12. Posts: 3

    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

    #
    12
    May 7, 2012 at 7:35 am
    • Posts: 1

      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.

      #
      12.1
      Jun 14, 2012 at 11:08 pm
  13. Posts: 26

    do I have to use the pro version?

    #
    13
    Jul 4, 2012 at 7:32 pm
    • Posts: 109

      No, you don’t; I simply forgot to switch the skin back after working in it. =) Sorry about the confusion!

      #
      13.1
      Jul 4, 2012 at 8:05 pm
  14. Posts: 26

    Oh, don’t worry, It’s okay!

    #
    14
    Jul 5, 2012 at 9:03 am
  15. Posts: 26

    But, how can I change my skin?

    #
    15
    Jul 5, 2012 at 9:03 am
    • Posts: 109

      This is only possible with Unity Pro; it let’s you choose between a dark and regular skin.

      #
      15.1
      Jul 5, 2012 at 11:03 am
  16. Posts: 26

    But wicht of those two versions are you using?

    #
    16
    Jul 5, 2012 at 1:36 pm
    • Posts: 109

      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.

      #
      16.1
      Jul 5, 2012 at 1:46 pm
  17. Posts: 26

    But can I still do the tutorial with the free one?

    #
    17
    Jul 6, 2012 at 7:15 am
    • Posts: 109

      Yes, you can. If a tutorial requires Unity Pro we’ll be sure to put it in the title/ first line of the description. =)

      #
      17.1
      Jul 6, 2012 at 7:29 pm
  18. Posts: 26

    Thanks, sorry for bothering you ;)

    #
    18
    Jul 8, 2012 at 8:27 pm
  19. Posts: 47

    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!

    #
    19
    Aug 15, 2012 at 9:27 am
  20. SamJ
    Posts: 1

    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?

    #
    20
    Jan 9, 2013 at 3:45 am

Leave a Comment

You must be logged in to post a comment.