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




Realy nice, but why you didn’t use Particle System insted of Particle Emitter ?
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!
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.
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!
Very nice tutorial! Helped me quite a bit on some things.
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
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!
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
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
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
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!
Good tutorial and a good series. Small enough to be approachable; but big enough to teach some useful skills.
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.
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.
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!
I cannot drop the Thrusters in the Particle Emitters
Hi there- I’m not quite sure what you mean, could you explain further?
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.
Having a similar issue. There’s no “emit” checkbox in the new particle system.
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!
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.
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.
no need for response got to the bottum of it.
thanx
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.
Actually, I’ve switched over to Debug.Log myself, since then. Mainly because the Print command won’t always show up, in certain cases.
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.
lol
when you say “Keeps it organized” the subtitle in youtube says “sex organs”
Yeah Youtube isn’t the best at captioning
-Alex
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:
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
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:
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 ._.”
Hey Jonathan! Very strange…perhaps you found a bug there, that’s definitely not normal behavior. Glad you got it working though!
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?
Could be an incorrect variable type, can we see your script?
you can post scripts on our forums with syntax highlighting.
-Alex
Thanks. I think your are right. I believe I had the var as ParticleEmitter and it should have been ParticleSystem.