unity 3d student

Challenge C01 (Beginner)

Using what you have learnt from the modules below, construct a game mechanic where the player presses the space bar and a box is fired at the wall to destroy it.

Good Luck! Post any questions you have about the challenge using the reply form below.

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

88 Comments

    First I would like to say thank you for the great lessons. I have been using unity for some time but there is so much I learned from these modules. Now I must say I am baffled by the first lesson. I’m pretty confident I can get everything working like in the video but I can’t seem to get the box to go in the direction I want it to. Any tips?

  • oh great! this is very motivating, tanx will you are Great!

  • Great content and style.

    Are you going to keep adding content as you develop it ?
    What recording software do you use (Camtasia Studio ?)

    Thanks very much indeed

    Cheers

    • Hi there! Yes I’ll definitely be adding more content soon! I use Screenflow for the Mac as there’s not much else that can touch it in terms of workflow and efficiency – I haven’t seen the latest Camtasia, so i’ll be curious to see what it can do, but I doubt it could be much better than Screenflow.

    Want to thank you for the free tutorials. They are very usefull and i’m glad you got this initiative. I’ve spent several weeks/months trying to gather tutorials so i can learn for my TBS simple project. Watching this modules , i figure out most of it in my head, now i have to try it. Hope in a future with more and more tutorials

    best regards

    • Julian, thanks for the comment, more modules are coming soon worry not, hope your project went well!

    Hi!

    I’m having some trouble in this challenge. Figured out the rest but this one issue is giving me headaches.

    The box I shoot by using space doesn’t bounce much on the floor and gets stuck before it can reach the wall. I tried experimenting with various values in the force that gives it the initial nudge but nothing seems to work.

    I narrowed it down to a problem – part of the box gets stuck in the floor itself. The colliders are properly set but the box goes through part of the floor. Any idea what I’m doing wrong? 🙂

    • Hmm, sounds odd, all I can think of it maybe you’re starting the spawn point too close to the ground so it is dug in upon instantiation? why not download some basic screen capture software and post a video of your problem on youtube and post a link to that here so we can all take a look? Thanks for the comment!

      • Thanks for the quick reply! I spawn it from an empty GameObject with a nudge on its Z axis but I don’t think it’s too close to the ground.

        Uploaded the video here – http://www.youtube.com/watch?v=hlDqoRi3rTE

        • Hi there, perfect! That’s exactly what I needed to see. The answer however is less clear. Why its intersecting the ground I have no idea – all settings seem standard and correct. As for making it do what you want for the challenge to succeed is consider the angle of the spawning object – it seems to point downwards at present, and also consider a much higher value of added force. Good luck! 🙂

          • I had it rotated downwards because I wanted to have it hit the floor first then the wall, like in the video. Now I pointed it straight and increased the force, it did indeed work. Glad I finished my first challenge alive! 🙂

            I think I’m gonna try reinstalling Unity to see if it fixes the colliders intersecting though. For some odd reason, the same thing happens in any project, even newly created ones.

    this is the best and the fastest way to learn unity, I’ve been spending over a year without learning anything, and you learnt me so much in like 2 hours

    thanks a lot

    • You’re welcome Simen, glad its working for you 🙂

    Best Training EVER MADE!! So easy to use, thank you so much!

    Moving to challenge number two 😉

    • Ace! Good luck! 😀

    Hello!

    Thanks for making such great tutorials, that’s pretty useful material!

    I’m doing this challenge now and wanted to push the envelope a bit further. Instead of destroying the wall with one single hit, I wanted to give some HP to the wall, so it takes like 3 hits for it to be destroyed.

    This is done but I also wanted to display the Hit Points in a GUI Text Element, which I cannot seem to be able to link to anything right now. How do you do that ? Do you have to create a global variable somewhere ?

    Right now, I can only track the number of hits via a Debug Log…

    Can you help me on this please ?

    Thank you very much! 🙂

    Philippe Côté

    • Hi Philippe, you’re right and thats a great way to expand this challenge! – take a look at Beginner module 14 and you should be able to work out how to figure that in to this one..

    The tricky part was realizing that I have to use an empty GameObject before I could see the cloned cube.

  • Hi!
    I’m using this script on the box variable to spawn it and to launch it towards the wall by pressing the “Jump” button which in this case is space bar.
    The problem is that I get a compile error that says something like “Expecting ‘(‘ found ‘Start’ in the first function Start part.
    Could you tell me what is wrong with this? Thank you.

    var thePrefab : GameObject;
    var power : float=500.0;

    function Update () {
    if(Input.GetButtonUp(“Jump”)); }{

    function Start () {
    var instance : gameObject = Instantiate(thePrefab, transform.position, transform.rotation);
    }
    function Start () {
    rigidbody.AddForce(Vector3(0,0,power));

    }
    }

    • Hi! You’ve placed a Start() function inside your Update() function… oh wait.. no.. you’ve placed two of them in there, not sure why. Put simply, Start is a function that you can use to call commands when the scene starts, and update is a function that runs every frame of the game – you cannot place them inside one another, they are separate entities. Then you’ve created a variable called instance but not used it to address the rigidbody you’re addressing lower down in your script. Ideally you don’t need any start functions at all if you’re just creating an instance of something then adding a force. The last issue is that you’ve said ‘var instance : gameObject’ – you need a Capital letter G for GameObject when specifying a data type, otherwise unity will read this as a reference to the gameObject the script is attached to.

    Hi Again… !
    I tried to fix the script using the tips you gave me. They were useful, but I have too less knowledge in these things so I couldn’t do it all the way.
    Here it is now, but it’s saying that it expects ‘(‘ but finds ‘;’ on the line 4

    var thePrefab : GameObject;

    function Update (){
    if(Input.GetButtonUp(“Jump”); {

    var instance : GameObject = Instantiate(thePrefab,transform.position,transform.rotation);
    instance.rigidbody.AddForce(Vector3(0,0,500.0));

    }
    }

    • in your IF statement, you’ve opened one two “(” and only closed one “)” So I assume what it actually says in the error report is that it expects “)” and finds “;” – semi colons are for the end of commands, not IF statements. So remove the semi colon, and place in an end bracket, you need to understand that in your IF statement there are brackets for the IF itself, then a second set for GetButtonUp(), so its like this –

      if( Input.GetButtonUp(Button here) ){
      //instructions
      }

    HEY ! I GOT IT! 😀
    Thanks for your support here. I can’t believe I really missed that close bracket, the error log said it was on another line in my opinion but it doesn’t matter anymore.
    Here it is !

    http://www.youtube.com/watch?v=l05zaMTcOxo

    Challenge 2 here I come!

  • Hi,
    I have the following problems:
    1. I don’t see how I can connect the Space button with the initiation/launch of the Instatiation.
    2. I can’t make the Wall disappear on collision (I managed to do what you show on the tutorial where the wall disappears after 3 seconds at Start, but I can’t connect the collision with the Destroy of Wall.

    Please help?

    PS: You are doing an amazing job!

  • Hi again,
    Please ignore my previous post. I did it!!! Going full-speed to the second challenge!
    Thank you for giving us this amazing knowledge!

    Panos

  • It took me about half an hour to figure it out. The hard part was figuring out how to apply the force to the rigidbody before the box was created. I simply put the force script on the box prefab, and basically combined the input, collision detection, and destroy script functions and placed that on the empty gameobject. Thanks Will! Nicely done!

  • Thanks! A straightforward but nice intro to RigidBodies and AddForce().

    http://www.youtube.com/watch?v=uK8KlhqE3b8

  • Thanks a lot for this website. I actually felt like I accomplished something with this first challenge as some of the ways to do a certain thing aren’t immediately clear. You have to figure out some things on your own, very cool.

    Beyond helpful, thanks 🙂

  • I think this site is a great resource, I just finished challenge one. It’s kind of like a wiki where you would look for info on how to do a certain thing, but instead of reading the article you’re watching a short video.

  • Mr. Goldstone, you’re a beast. We’re using Unity at Uni this year. We’re having to make an RPG game for one of our modules and your tuts are a great help. NOw I’m not the best at coding I have to admit but you’ve made me see that with little coding you can get big results. All I can say is thank you very much for the time and effort you put into this site.

    P.S. You’re Unity Essentials book is awesome too.

  • Thanks so much for these tutorials – I’m teaching a digital art course and included Unity as one section, and these help both myself and my students. And your book is great too! A great way to talk about scripting-as-art!

    I’ve figured out challenge 1, but I tried to make an additional effect for the disappearing wall. I’d like the wall “sink” into the floor slowly and then disappear (thinking of old World of Warcraft deaths!). Sort of got it working, but the movement seems chunky and translation is confusing.

    Here’s the script I have on the wall:

    var speed:float = 5.0;

    function OnCollisionEnter (col : Collision) {
    //arbitrary wait for falling down
    yield WaitForSeconds(3);
    Destroy(rigidbody);
    transform.Translate(Vector3(0,0,-speed)*Time.deltaTime);
    yield WaitForSeconds(3);
    Destroy(gameObject);

    }

    The other thing you’ll notice is the transform problem – in this case the world translate thing is confusing to me – if I have it translate in Y, the local translate moves the now-fallen wall forward. So I have it translate in Z, but it would be nice to have it more generic and reproducible for other objects.

    Sorry for the long post, I understand if you can’t get to this. I’ll keep plugging away.

    • Hi Pat, thanks for the message, always cool to hear someone taking the challenges further, that’s why they’re there! Anyway to achieve what you’re doing it’d be simplest to retain the wall’s rigidbody, but then upon collision switch the wall’s collider to trigger mode, something like-

      gameObject.AddComponent(Rigidbody);
      collider.isTrigger=true;
      Destroy(gameObject, 3);

      So adding a rigidbody, stopping the collider reacting by making it a trigger then destroying after 3 seconds.

      Let me know how you get on!

      • thanks! elegant solution of course, got it to work – here’s the script change I made; included a check that the floor itself isn’t the collision:

        function OnCollisionEnter (col : Collision) {
        if(col.gameObject.name != “floorPrefab”){
        yield WaitForSeconds(3);
        collider.isTrigger=true;
        Destroy(gameObject, 3);
        }
        }
        here’s the result in all its glory…
        http://www.youtube.com/watch?v=J1O722x0eac

        time to share it with students! thanks again!

        • Pat – thats great. Bear in mind if you a) removed the yield and b) destroyed the boxes that hit the wall upon the collision you’d get-

          – box hits wall, but doesn’t fall over as you’d destroy the box before adding a rigidbody – you could also fix the rotation on the rigidbody that you add.

          – wall starts sinking immediately.

          worth a try?

    Hey Mr. Goldstone! I just wanted to say thanks for the effort you have put into these tutorials! I’m 14 years old and just started with Unity and scripting, but your tutorials have helped me so much!

    Thanks again!

    • Curt – great to hear, always good to hear younger people getting started in game development. What kinds of things are you finding difficult so far?

    super….motivational

    • thanks! hope you enjoy it!

    I am finding that when I rotate using the cube to face the direction I want it to using the rotate tool (Hotkey: E) then swapping back to the translate tool (Hotkey: W) the axes are not changing to match the orientation of the object. Has anyone else experienced this problem/bug? The axes point in the correct directions when I change to the scale tool (Hotkey: R) though.

  • Thanks for the great web site Will. I really enjoy the format with the shorter videos that focus upon one specific topic. I find it easier to digest and remember the material when it is presented this way.

    I completed the first programming challenge and used the opportunity to also learn how to create and publish a video tutorial which demonstrates my version of the solution along with an explanation of how it was implemented. The video is here: http://vimeo.com/18833048

    Thanks again!

    On to challenge #2…

  • The only difficulty I’ve encountered while doing this challenge is unity’s naming convention.

    For example GameObject sometimes comes with a capital G, while sometimes it is used as gameObject, with a lower case g.

    Is there any way to easily identify when a capital should be used?

  • Try and think of capital G when referring to data types, eg in establishing a variable you’d say var something : GameObject; whereas when referring to a particular object you might then use lowercase eg gameObject.name=”something”;

    • Thanks for the quick reply 🙂

      I have 2 more questions:

      1. I keep seeing you renaming scripts by simply clicking on its name in the project panel.
      Doesn’t seem to work for me. Tried right click, left click and even the mouse button.

      2. How do you remove components? I’ve tried added an material to the cube, but couldn’t find any nice textures/shaders, so I decided to leave the cube plain. Can’t seem to remove the material component on the cube prefab. Am I supposed to right click somewhere?

      Edit: I hope this message doesn’t show up 3 times or something… Can’t seem to post

      • Simon – press return (mac) or F2 (pc) to rename things, same as the OS.

        To remove components just click on the cog in the top right of the component and choose ‘remove component’.

    Im Having problems with the scripting. I have no idea what to type in the sripts. I watched the videos but it still doesn’t make sense.

    • Not sure what to tell you Josh.. bear in mind when getting into game development, you don’t necessarily need to be a programmer, have you explored working with 3D modelling packages also? Don’t be discouraged!

    I DID IT!!! I found 4 different ways to do it xD but at the begining it was very very difficult. but testing here, there and over there, making mistakes its a realy nice way to learn =) nice tutorials!!

    waiting for more modules and challenges…

    can you make a tutorial of importing blender models in .fbx and how to play animations of that model? that will be a very nice one!!

  • My entry:
    http://www.youtube.com/watch?v=bagqHgRognE
    Short explanation found in description of Video (along Credits & links).

  • Hi thanks for taking the time to explain this to me. I am new to Unity3d and I wanted to go and set up a counter, one that would count how many boxes where shot, how would that be accomplished? Again thanks for any help.

  • Hello! Let me begin with saying that your tutorials have very helpful and nicely done. Lets go to the main problem now..

    I have been trying to figure out the solution of the challenge and i cant seem to make the collision work.

    This is my code for the bouncy box. This part works fine. The box is created and thrown towards the wall.

    var power : float = 500.0;
    var BouncyBox : GameObject;

    function Update ()
    {
    if(Input.GetButtonUp(“Jump”))
    {
    Debug.Log(“Launch!”);
    var instance : GameObject = Instantiate(BouncyBox, transform.position, transform.rotation);
    instance.rigidbody.AddForce(Vector3(power,0,0));
    }
    }

    Then, this is my collision function

    function OnCollisionEnter (theCollision : Collision)
    {
    if(theCollision.gameObject.name == “BouncyBox”)
    {
    Destroy(gameObject.Find(“Wall”));
    Debug.Log(“We have hit the wall”);
    }
    }

    This part Does not work.. :/ i have tried to attach this function to the script of the cube as well as to the wall. However it doesnt work. Whats my problem doctor??

    Thank you so much! Keep up the good work!

  • Yay i did it , in 30 mins im proud of myself!

    Thanks for the great tuts

  • hi im having trouble ive done all tutorials up to here but i dont understand how im ment to press the space bare and thenn the box have force have i missed something?

  • I finally did something write in unity without copying someone elses code 🙂 🙂 🙂

  • Hi,
    Can you make module about 1ST person camera.

    THANKS

  • Thank you for the challenge !

    My thrown box was able to destroy the wall, however it looks like the box was flying, not being thrown because of added force every time Update() is called, and that’s weird in my eyes. Anyone knows how to fix this problem ?

    • Firstly, Thanks for this challenge and your modules overall. They’ve been more helpful than any script examples I could find.

      Again Thank you for the Lessons.

      @Yuu, I set my force applied to a Function Start so it’ll only apply once

      If you don’t want to ruin the challenge for yourself, don’t read after this line.

      I have a script to spawn the box attached to an empty gameObject with the object to spawn an exposed variable.
      Then a script that holds all the behaviors of the box(force and collision detection) attached to the box prefab

    First of all thanks for the tutorials so far they seem good.
    Im all new to unity and so far my experience is limited to your lessons.

    I have a problem with the first challenge.
    When i press space it creates the box but only the EmptyGameObject gets the force added?

    Here is my code so far which is attached to the emptyGameObject

    var thePrefab : GameObject;
    var power : float = 500.0;

    function Update () {
    if(Input.GetButtonUp(“Jump”)){
    var instance : GameObject = Instantiate(thePrefab,transform.position,transform.rotation);
    gameObject.AddComponent(Rigidbody);
    rigidbody.AddForce(Vector3(0,0,power));
    }
    }

    • gameObject.AddComponent(Rigidbody);
      rigidbody.AddForce(Vector3(0,0,power));

      should be

      instance.AddComponent(Rigidbody);
      instance.rigidbody.AddForce(Vector3(0,0,power));

      because instance is the variable you are creating to represent the thing you just created. If you write gameObject by itself, you are referring to the object the script is attached to, hence why the empty object would have the force applied to it, because in your script you were adding a rigidbody to it, and then adding a force. Note also that you needn’t write instance.gameObject.AddComponent because instance is set to be a GameObject as its type.

    Thanks, i managed to make a working solution now but i do have some more questions.

    Here is my collision function which i attached to the wall

    function OnCollisionEnter (theCollision : Collision)
    {
    if(theCollision.gameObject.name == “crate(Clone)”)
    {
    Destroy(gameObject.Find(“Wall”));
    }
    }

    1) Is there something wrong somewhere since i have to add the (Clone)?

    2) I only could get this function working by attaching it to the Wall but wouldnt it be better if it was attached to the cube or should it be a attached to something else?

    3) In general can i have more then 1 function in each script?

  • O k what ever i do when i write a script put a rigbody on the object and it has mesh stuff on it when i start it it just falls through the floor PLEASE help 🙁

    • You are probably starting the scene with colliders intersecting, OR trying to get two mesh colliders to interact – in order for this to work you’ll need to check ‘convex’ on one of the two mesh colliders.

      • no mesh colliders or any starting togethe just box colliders :.(

    Hi. Great Tutorials. I am currently studying games development in college (off for the summer now though). I will tell my professors and my peers about this site when I go back.

    Anyway, I have a question about this challenge. I managed to get it working but I was wondering if there was a better way than the way I used for the collision.

    Basically I had to specify in a javascript that the collision involved a Clone.

    if(theCollision.gameObject.name == “boxPrefab(Clone)”)
    {
    do other things;
    }
    Like I said, it works but I feel like it could have been coded better.

    Could you let me know if this is the right way?

  • WG, we want more challenges … thanks !

  • how do i get the wall to be destoyed when the boxprefab hits it?

    i add this to collision detection script and not working help pls

    Destroy(gameObject.Find(“Wall”), 3);

    do i need to make a tag?

  • how do i get the wall to be destoyed when the boxprefab hits it?

    i add this to collision detection script and not working help pls

    Destroy(gameObject.Find(“Wall”), 3);

    do i need to make a tag?

  • nvm got it. heres the script

    function OnCollisionEnter(collision : Collision)
    {
    if (collision.gameObject.name == “Wall”)
    {
    Destroy(collision.gameObject);
    }
    }

    before i was doing gameobject.find mabey that was the problem

    • Yes, you should be using the reference to the collision, not Find()

    • I don’t think anyone will reply to this but, I tried many different things to get my wall to be destroyed, I even tried your exact same script and it did not work. Can someone please help me?

    My way of saying thanks for the awesome tutorials: http://www.youtube.com/watch?v=1Wea0AYJ7zk

  • Thanks for the awesome tuts. I got it done it about 45 mins.

  • Just a question. Let’s say I to use this same method to fire a bullet. How would I instantiate the bullet at the tip of the gun (which would be the object that has the script component)?

    I assume I would have to change the second parameter in the Instantiate() function.

    Any advice?

  • Thanks for the great tutorials here. Love it! Hope to see more in future. Will try this challenge out. 😀

  • hi man,

    It was actually a very easy challenge but I don’t know how to script the destroying part.

    i made bounce, appear out of nowhere using the spacebar and I even made it say “hit the wall” but the destroying part is still not working and i don’t know what the problem is.

    this is what I used for that part:

    function OnCollisionEnter(theCollision : Collision){
    if(theCollision.gameObject.name == “wall”){
    Destroy(gameObject.Find(“wall”));
    }
    }

    i have already checked it on capital letters and stuff

    greetings,

    Dinand

  • hi man,

    It was actually a very easy challenge but I don’t know how to script the destroying part.

    i made bounce, appear out of nowhere using the spacebar and I even made it say “hit the wall” but the destroying part is still not working and i don’t know what the problem is.

    this is what I used for that part:

    function OnCollisionEnter(theCollision : Collision){
    if(theCollision.gameObject.name == “wall”){
    Destroy(gameObject.Find(“wall”));
    }
    }

    i have already checked it on capital letters and stuff

    greetings,

    Dinand

  • ow i’m sorry, i manage now

    thanks anyhow

    good luck on tutorial making

  • Hi I just got unity I stayed up all night trying this challange but my java script is not working please help also i cant seem to get the wall to disappear when I hit it

    function Update () {

    if(Input,GetButtonup(“jump”)=true then{
    var thePrefab : GameObject;

    function Start () {

    var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);

    }}

  • I just completed this exercise. It wasnt hard at all but thats probably with my previous coding experience. Thanks for the great tutorials.

  • Hello. I’m having difficulty trying to make the box appear when I press space. If I put the creator script into the getbuttonup code, it doesn’t work properly. I can only get the box to appear on its own for now. Here is my script:

    var power : float = 500.0;
    var thePrefab : GameObject;

    //This portion is supposed to make a cube once you press the space button. At the same time the cube is being launched into a wall
    function Start ()
    {
    var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
    }

    function Update()
    {
    if(Input.GetButtonUp(“Jump”))
    {
    rigidbody.AddForce(Vector3(0,0,power));
    }
    }

    //This OnCollision code is supposed to cause both the wall and cube to be destroyed and the text, destroyed the wall is logged in
    function OnCollisionEnter(theCollision : Collision)
    {
    if(theCollision.gameObject.name == “Floor”)
    {
    Debug.Log(“Hit the floor”);
    }
    else if(theCollision.gameObject.name == “Wall”)
    {
    Destroy(gameObject.Find(“Wall”));
    Debug.Log(“Destroyed the Wall”);
    }
    }

    If anyone could help me make the box appear when I press space I’d appreciate it.

  • Tutorials are extremely helpful. Good stuff!

  • Will, you are awesome !

    We are so lucky to have your training and assistance.

    Thank you, thank you.

  • Hi, Great tutorials! I am just getting started with Unity and finding your tutorials to be a great help.

    I just finished solving this but am having a tiny issue with destroying the wall.

    I am using:

    function OnCollisionExit (theCollision : Collision) {

    if (theCollision.gameObject.name == “Action Cube(Clone)”) {
    Destroy(gameObject);
    }
    }

    In the destroyer script and have attached it to the wall. The problem is, there is a short delay between when the cube hits the wall and the wall disappearing. I notice in your video, these two events seem to happen simultaneously. Any idea what is wrong?

  • Hi, this site has been the closest to anything i can find on the entire net to get me even started with javascript but that’s just the problem. I don’t know whether it’s if I’m terrible at learning this stuff but i can never even start to type any script. to tell the truth I’m having huge problems trying to grasp onto the language and I’m told time and time again it’s one of the most simplest scripting languages to learn. All the functions and ifs and “{“s are just melting my mind, even after cycling through these tutorials again and again until my mind is numb. it kinda feels like I’m a new born baby with no language skills pushed into a primary school and forced to write my first story. some one please! I need help from scratch-scratch not “from scratch”

  • Hello, I just started learning to code using unity and wonder how would code look for Challenge C01 (Beginner)
    Thank you.

  • Hello Will,

    i am having trouble in this challenge.

    i am unable to hold my box in the air when the game starts, whenever the game is started the box is falling down to the floor because of the gravity.how to old the box when the game start. apart for this i have figured out everything.

    my instantiate code is in start method, attached to a empty game object.

    Please help me out will .

    • i Have found the solution Will. And finished the 2nd challenge also.

      After Spending some months in all kinds of Tutorials (not yours WILL), I have decided to quit learning Unity as i am Frustrated that not learned anything .

      But you made me come back Again , And given me strength . After finishing 2 Challenges , I am feeling so confident.

      Thank q so much Will, U Made My FUTURE. 😀

    I still dont get why you waste your valuable time and teach us noobs in such an awesome way ?

    Thank you so much mate. I mean seriously you have been a great help to us 🙂

    Keep this awesome work continue for nabs like us lol

Leave a Comment