Unity Scripting: Scripting the Player Controller

In this tutorial, we will finally get that Lander pod moving, and looking pretty! You’ll learn how to do some simple, but very useful, scripting, including accessing components, physics, and inspector-editable variables. By the end of this tutorial, you’ll have your ship flying about the level, with particle effects to match.

Also, I’ve decided to go ahead and provide all the real, much awesomer, painstakingly-hand-crafted models that will be in the full Mine Lander game once I launch it. You can even feel free to use these models however you like, just please credit me is all I ask. Of course, if you created your own, more power to you!

What is covered in this tutorial:

  • - Creating a Javascript file
  • - Attaching scripts to objects
  • - Scripting for physics-based controls
  • - Attaching external components to your script
  • - Axis-based input via scripting
  • - Other scripting-oriented useful tips

Play Lunar Lander:

Lunar Lander<>

Discussion

36 Responses to “Lunar Lander 03: Scripting the Player Controller”
  1. Posts: 5

    Realy nice, but why you didn’t use Particle System insted of Particle Emitter ?

    #
    1
    Mar 11, 2012 at 1:16 am
    • Posts: 268

      Hi Povtixblend, glad you liked it. I used an old-school Particle Emitter because these are particle effects that I had created before Unity 3.5. No reason to throw away effects if they still work great! :)

      #
      1.1
      Mar 13, 2012 at 11:01 pm
  2. Having my java and making java (input.addhalfandhalf(drinkitdown)==:) Just wondering when do you add in say the thrusters sound effects/audio files?I am sure it is coming in 1 of the next tutorials but seems it should of been mentioned or child of thrusters or in with this script?also what format should the file be imported?just a thought,as i am a UN_1 (Unity Noob_Lv1) great tut as i am picking up what your laying down very clearly.

    #
    2
    Mar 11, 2012 at 8:46 am
    • Posts: 268

      Hi Ballistic! We’ll be going over audio in a separate tutorial, there is a lot there that wouldn’t quite fit into this one.

      Really good to hear it’s working well for you, as a starting coder/Unity user, that’s the aim!

      #
      2.1
      Mar 13, 2012 at 11:02 pm
  3. Posts: 14

    Very nice tutorial! Helped me quite a bit on some things.

    #
    3
    Mar 11, 2012 at 10:53 am
  4. carlos santos
    Posts: 3

    Nice tutorial. Can you tell me, should I use this approach of moving gameobjects (player and enemies) with physics or should I use transforms to develop for android and ios

    #
    4
    Mar 11, 2012 at 12:23 pm
    • Posts: 268

      I’ve actually ported this game to Android (it’s almost done, whoohoo!), and it runs great with a few adjustments. Physics are just fine even on lower-end phones, as long as you don’t go crazy with them or have a lot of visuals, audio, etc- it’s all about just testing and finding where the limits are. Not using would physics would definitely free up performance for other areas, but not necessarily a huge amount, and it was something I knew I wanted in this game. Thanks!

      #
      4.1
      Mar 13, 2012 at 11:05 pm
      • carlos santos
        Posts: 3

        tks for the explanation. I’m doing a “space invaders” like game, and I end up building a physics based controller and other controller just using the transforms for the player I was wandering who’s the best to use. I guess i’ll stay with the physics for the player and use transforms in the enemies. your explanation was pretty helpful. ty

        #
        Mar 14, 2012 at 10:57 am
      • Posts: 3

        im haven problems with mesh collider. box, sphere, and capsule colliders are fine. butt i dont know how to make my own custom mesh collider.

        thanx

        #
        Aug 26, 2012 at 2:03 pm
  5. Posts: 35

    Another nice tutorial Gabriel.

    You do sound almost apologetic for the “scripting”. Just remember, without scripting all you have is a rather low level render of a scene.

    I’ve taken some courses in programming, written a couple of java applets, even written with javascript/php for web pages – but that was a while back and this is way more sophisticated. So a relearning exercise.

    And as always, this tut gave me something to think about – the communication with the thrusters – I had been using SendMessage() for things like that.

    That same little piece of the tutorial also reinforces the need to make sure your logic in scripting is well thought out – “left” thruster moves ship “right” ;-)

    Can hardly wait for the next tut in this series.

    gryff :)

    #
    5
    Mar 12, 2012 at 10:29 pm
    • Posts: 268

      Hi Gryffdd, thanks again! Do I? Ha…perhaps because I always hated running into that as a new learner, but eventually overcame it. I want first-timers and the like to overcome that fear (or, it was for me), give it a try, and realize how easy and fun Unity makes scripting.

      SendMessage(), yeah, it’s good to find simpler, more straightforward methods whenever possible! I’m sure a more advanced coder to find all sorts of inefficiencies in my code, lol, I’m always learning too!

      #
      5.1
      Mar 13, 2012 at 11:10 pm
  6. Posts: 3

    Good tutorial and a good series. Small enough to be approachable; but big enough to teach some useful skills.

    #
    6
    Mar 13, 2012 at 4:20 pm
  7. Gary Parkin
    Posts: 33

    Thanks Gabriel, for the in depth explanation on where things get things from. Like where the controllers are kept, and how to access the rigidbody parts. I code professionally as a living (C#,VB,JS)and this makes it easy to understand the Unity object system.
    I like the particles. I’m thinking of using them in a render or maybe porting it as film to AE.

    #
    7
    Mar 14, 2012 at 11:49 am
  8. Hello every one i like this tutorial really good explain am thinking to use this tutorial to make a small game will credit work to you if i don’t use my own prefab.

    Any way i think i will use some of yours.At soon i get some don will let you know.

    #
    8
    Apr 28, 2012 at 2:34 pm
    • Posts: 268

      Hi Nateao-

      Sure thing! You can just credit me here at UnityCookie- thanks for asking :)
      This is incentive for ME to actually finish the full game soon, ha!

      #
      8.1
      Apr 29, 2012 at 12:52 pm
  9. Posts: 2

    I cannot drop the Thrusters in the Particle Emitters

    #
    9
    May 19, 2012 at 6:56 am
    • Posts: 268

      Hi there- I’m not quite sure what you mean, could you explain further? :)

      #
      9.1
      May 20, 2012 at 6:46 pm
      • Posts: 2

        I have a newer version of Unity and the ‘particle system’ is different. Could you help me and tell me if I have to do or write in the script something different.

        Thank You.

        #
        May 21, 2012 at 6:54 am
      • Posts: 7

        Having a similar issue. There’s no “emit” checkbox in the new particle system.

        #
        Jun 24, 2012 at 7:55 am
    • Posts: 1

      I Know what you guys mean, first of all you will need to Change var topThruster : ParticleEmitter to var topThruster : ParticleSystem; The systax wont highlight if you’re using monodevelop like me,

      And instead of topThruster.emit = true; will now need to be
      topThruster.Play();

      and use Stop(); accordingly. Hope this helps ya’ll!

      #
      9.2
      Jul 4, 2012 at 9:56 am
      • Posts: 268

        Hi Steve, thanks for the input! Just to clarify, that code will work with the “new” shuriken particle system, vs the code in the tutorial is for the “old” particle system.

        #
        Jul 4, 2012 at 10:00 am
  10. Cesar Fragoso
    Posts: 2

    Hi Gabriel,

    Nice tutorials, can you tell me if I would want compile all the project for android, what changes will must do it?.

    Thx.

    #
    10
    Jul 4, 2012 at 2:42 pm
  11. Posts: 3

    no need for response got to the bottum of it.

    thanx

    #
    11
    Aug 26, 2012 at 2:37 pm
  12. Posts: 1

    Instead of using print(“Hello”); — why don’t you use Debug.Log(“Hello”); — is there a difference? I have just always been taught to use Debug for debugging purposes – because you can also called Debug.Drawline, Debug.DrawRay … etc.

    #
    12
    Sep 21, 2012 at 9:21 pm
    • Posts: 268

      Actually, I’ve switched over to Debug.Log myself, since then. Mainly because the Print command won’t always show up, in certain cases.

      #
      12.1
      Sep 21, 2012 at 9:59 pm
  13. Posts: 47

    Why aren’t you putting all the Movement within function FixedUpdate()? From the Unity Script Reference it states that Rigidbody should always be used within FixedUpdate instead of Update.

    #
    13
    Nov 27, 2012 at 10:37 am
  14. Posts: 6

    lol

    when you say “Keeps it organized” the subtitle in youtube says “sex organs”

    #
    14
    Dec 7, 2012 at 3:15 pm
  15. Posts: 12

    hmm… Since I did my particles with a particle system I can´t find a way to make it work, here’s my script: (I did assign the topThruster particle system to my variable)

    var TopThruster : ParticleSystem;
    function Start(){
    TopThruster = GetComponent(ParticleSystem);
    }
    function Update()
    {
    if(Input.GetAxis(“Vertical”) > 0){
    print(“Pressing”);
    TopThruster.enableEmission = true;
    }
    if(Input.GetAxis(“Vertical”) == 0){
    print(“Pressing”);
    TopThruster.enableEmission = false;
    }
    {

    any ideas? D:

    #
    15
    Mar 1, 2013 at 9:02 pm
    • Posts: 268

      Hi there! With the Shuriken system, you’ll need to use “.Play()” and “.Stop()”. Here’s the info link:
      http://docs.unity3d.com/Documentation/ScriptReference/ParticleSystem.Play.html

      #
      15.1
      Mar 1, 2013 at 9:47 pm
      • Posts: 12

        I tried that one already, but keeps telling me “NullReferenceException: Object reference not set to an instance of an object” for both of them… so stressful D:

        #
        Mar 1, 2013 at 11:07 pm
      • Posts: 12

        haha sorry for spamming… I manage to make it work, but I had to include the same script to the thruster object, its like it wont recognize the particle system if the script isn’t attached to the particle system itself so Ill have an script for each one of my particle system ._.”

        #
        Mar 2, 2013 at 1:33 pm
      • Posts: 268

        Hey Jonathan! Very strange…perhaps you found a bug there, that’s definitely not normal behavior. Glad you got it working though! :)

        #
        Mar 2, 2013 at 2:48 pm
  16. Greg
    Posts: 2

    I cannot get the thruster variables to be assigned via drag-and-drop in the inspector. It just shows the circle with a line through it like that’s not allowed. What am I doing wrong?

    #
    16
    Mar 11, 2013 at 5:26 pm
    • Posts: 147

      Could be an incorrect variable type, can we see your script?
      you can post scripts on our forums with syntax highlighting.
      -Alex

      #
      16.1
      Mar 12, 2013 at 6:35 am
      • Greg
        Posts: 2

        Thanks. I think your are right. I believe I had the var as ParticleEmitter and it should have been ParticleSystem.

        #
        Mar 13, 2013 at 10:28 am

Leave a Comment

You must be logged in to post a comment.