unity 3d student

Challenge C03 (Beginner)

Create a game mechanic where the player can aim left and right, and shoot at 3 targets, when shot, they should fall down and the player should be awarded a point. When the player reaches 3 points, they should be taken to the game over scene.

Good Luck!

Share via Social Media

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

63 Comments

    Got them done. The hardest part was probably finding how to use global variables between scripts, but you probably did it in a much more practical fashion. Ah well, I’m not a programmer, but this is helping me greatly. Keep em’ coming.

    • Well done! More challenges coming as I’ve started to continue developing the site this week..

      • this is too hard it doesnt make scence

    I enjoyed all 3 challenges very much, thanks for your great work!
    I hope you find some time to do some more (advanced) challenges and maybe some solutions for them too.

    • Hi Michael, I’ll be definitely doing more videos but I may not be doing solutions – that’s everyone else’s job! in all seriousness, the point of this programme is to put the emphasis on learning by problem solving so I hope to keep and support that ethos where possible.

    Love these tutorials! I’m just starting out with Unity, and I’m having some trouble getting the GUI text script to detect collision and calculate the score. Any ideas as to the codes I would use?
    Thanks

    • What did you try so far? the point of the site is not to show solutions so students can learn by solving the problems, but i’ll be happy to give hints / clues!

    Hi, am just sstarting out in UNITY as well , and think this way of teaching it works much better than just showing the solutions, think this would work great, and hope this site grows, cant wait till the next challange!

  • did it by using global variables. Noticed that globals are not covered in the tutorials, at least as far as I could tell. So that leads me to think: is there another way to do it without globals?? Just a yes/no answer is all I need to get back to researching it.

    • No, global variables are not required, and I didn’t use any in my solution. Public member variables work fine. If you really wanted to, there’s no reason you couldn’t use private variables with accessors, although that would be going overboard for such a small project in my estimation.

    Thank you so much for your tutorials! They were a great place to start to help piece together all the different concepts I have picked up at various websites in a straight forward and helpful way. Please keep them coming!
    Thanks for all your work,
    Justin

  • Will,

    I’ve sorta completed the challenge. I tried to combine as many things as I could. Thought I left out the load scene part.

    http://www.endbossgames.com/unity/C01.html

    thanks!

    Can’t wait for the intermediate ones!

    • Haha, I had some fun playing with your version of the solution. Nicely done mrTofu. :)

    • Very unique! :)

    Yay! I finished this challenge without any trouble. Gotta love the B17 module. :D

  • Thank you for the modules!. Im waiting for new tutorials and more challenges ^^. I´ve upload the Challenges here http://aburrimiento.hostzi.com/Challenge/BeginnerExaman.html
    Thank you again and keep working!

  • I have make a game with the things I learnend in the modules.
    Mouve the player with mouse clicking and shoot with the space bar. Have fun!
    http://aburrimiento.hostzi.com/Estrategia/JuegoEstrategia.html

  • Hello Will!

    I’m trying to do an alternative to counters to determine winning conditions. I want to use a true/false system but I’ve been searching for about 4 hours now and I’m giving up.

    I’m also struggling to target specific objects with a script. Unity always return me errors because I either use the wrong method or wrong syntax. I also don’t know if I can target the object directly or if I have to target the script inside an object. Also, since my objects are instances of the same Prefab, I don’t know how to target each specific one, even though I renamed them in the Hierarchy. It’s pretty simple in Flash’s ActionScript but totally confusing in Unity.

    Could you help me with those two problems, please ? It’s very frustrating and time consuming…

    Thank you very much.
    Philippe Côté

    • Philippe, some hints:

      (1) Watch this site’s video on accessing objects. I didn’t, but the rest were good, so I assume that one is! I *know* the Unity scripting documentation has a big section explaining accessing objects. A couple options are the GameObject’s Find() method at runtime or setting a public GameObject variable at compiletime and then pointing it to the instance you want. In C# you may have to typecast.

      (2) Well, you can access the GameObject AND you can access a Component within the game object. Usually, you want to access an object and then its component (like a script). That’s where the goodies are.

      Coming from a C/C++ background, I’m finding Unity’s C# totally intuitive and better documentated than most APIs I’ve worked with that are targetted towards more seasoned programmers.

    I really enjoyed those tutorials. I hope new tutorials available soon. Good work!

    • Thanks very much Nicholas, will put more up as soon as I have any spare time!

    I wanted to share my masterpiece solution that rivals Crysis, and strongly encourage the author to write more challenges. This is a wonderful way to learn and reminds me of a good intro to programming class. :)

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

  • First thanks for all of those funny lessons.

    About this Challenge, I’ve tried to use only what yours tutorials speak about and complete it with reflexion and Unity documentation.

    The only one thing I must use out from lessons is how to increment the score by using “myScore ++;”. Another way must be by test every frame something on target cubes to check if they were already hited. I simply think it is a fat method.

    in waiting the next, good vibes

  • FINALLY COMPLETED THE CHALLENGE!!!..

    Was stuck on the scoring system for hours and then got even more stuck with the damned level change..

    Thought i would never finish that!!.

    Only one problem i’m experiencing. When I make a final standalone build of my game, my guy moves wwaaayyy faster than it does in unity.

    The script i used was..

    function Update () {
    var horiz : float = Input.GetAxis(“Horizontal”)*.1;
    transform.Translate(Vector3(0,0,horiz));
    }

    Any ideas?

    • A good practice to move something by scripting is using a time notion like it is described in the tutorial “Beginner B07 – Basic Translate Movement” : http://www.unity3dstudent.com/2010/07/beginner-b07-basic-translate-movement/

      Then you will have the same speed between in-unity and standalone version.

      More generally this is the only way to have same speed between any configuration (processor power and else). This is named real time. Because after the same time of moving for two contexts (in-unity and standalone version here), this movement is the same. Only the count of frame (fps) could change.

    Hi There !
    ive almost figured everything out.
    theres just 1 thing wich propably also counts in your example.
    i have 4 hitboxes that can be hit, a rigidbody is added so they fall down.
    but… sometimes when they are hit, more points are added. and when my
    projectile used for shooting hits it it falls allong with the hitbox.
    so it hits the same box multiple times,
    and ive tried to go around this by using a destroy(collider) script
    but unity no longer recoqnises the destroy command it says :/
    here are my scripts used :

    –hitscore script ( no error ) :

    var Counter : int = 0;
    function Update () {
    guiText.text = “HitCount: “+Counter;
    }

    –hitcount script :

    function OnCollisionEnter (myCollision : Collision) {
    if(myCollision.gameObject.name == “hitbox”){
    var MyScore : hitscore = gameObject.Find(“hitscore”).GetComponent(hitscore);
    MyScore.Counter++;
    }
    }

    –hitboxdestroy script : ( error << ):

    function OnCollisionEnter (myCollision : Collision) {
    if(myCollision.gameObject.name == "hitbox"){
    if(!myCollision.gameObject.rigidbody){
    myCollision.gameObject.AddComponent(Rigidbody);
    Destroy (BoxCollider);
    }
    }
    }

    ive been looking in multiple ways of doing this, :S
    ive also thought to maybe put a max count of +1 per box,
    but i am not good enough in scripting to figure it out.
    (trust me ive tried, :P 20 errors later.. )

    hope you guyz can help me,
    maybe any insight in the destroy/removing of components
    or some further script example of a way to get a max count of +1 per block?
    thanks for the help,
    great learning vids!
    il try making a video for youtube
    so u can see my progress.
    greetz
    Dion

  • Hi

    First, thanks for this awesome tutorials to all members in crew unity3dstudent.com

    I have started as green noob :) And now I make my first game in unity.

    MetalHit 1.1.

    Controls:
    arrows keys (left, right), SPACE for fire.

    last target 3 points
    middle target 2 points
    firts target 1 point

    Get Challange and try to get into Hall of Fame :) )

    http://95.173.214.143/metalhit/metalhit.html

    notice: a lot of inspire from mr.Tofu | thanks |
    P.s. sorry for my English :)

    • Hi PeTak – that’s great! Really great to see how people are getting on using the site – so did you have any knowledge of 3D, Unity or game development before starting working with this website? – the team – just me – says congratulations! :)

    do you know if there will be any more tutorials or challenges coming up in the future. i am really eager to continue to learn how to use unity so i can make my own game.

    • There certainly will be, as soon as I have time to make them! Don’t wait for me to continue learning though – set yourself a task and aim to make it, if you don’t know what you need to do, then ask either on here or on the unity forums or IRC channel, see the ‘Get Help!’ page for details, good luck!

    how would i go about adding speed to an object i just instantiated from a empty game object?

    • Depends upon the context- either you’re using rigid body objects or not, if not then you could control the translation of the object within Update(), chances are you will want to use rigidbodies and control their velocity parameter or use MovePosition to move them around if they are kinematic- see the component manual on the rigidbody component if you are unfamiliar with these terms, good luck!

    Just finished this challenge, plus some extras: http://www.annamattaar.nl/zooi/TargetsWeb/WebPlayer.html
    (Sounds stolen from some Star Wars game and Rayman 3)

    I really love your lessons, this method works a lot better than those tutorials that only teach you to copy settings from a screenshot ;-) Going to watch a few more modules tomorrow and invent my own challenge 4.

    • Excellent, Good work Anna! Glad you like the tutorial system, I’ll try and take time to add more content soon – please post the next challenge you set yourself, I’ll be interested to see what you suggest!

      • I’ll post it. Probably won’t be tomorrow though, since someone invited me to the Middle Ages for a few days and they don’t have computers there… :P

        I do have a question in the meantime. I have some programming experience and I’m used to having some global script that’s not attached to any specific object, but controls the whole game/scene. I missed it here because it’s often the most logical place for score and win conditions. Is there something like that in Unity? Should I make one myself by attaching it to an empty game object? Or is there a better way?

        • That’s usually the way, though its worth putting them on an object you know will always be in the scene as each new transform added means another draw call – something you may wish to avoid , especially in mobile development. Anyway good luck with the work, enjoy the trip, hope the Doc promised you you’ll be back in time.

    Hey all :)

    I got a strange problem here.
    I’ve thought of a way to power up the shot. So I’ve added this in my BallProperties-Script:

    gameObject.rigidbody.AddForce(0,0,speed);

    In my PlayerInterface-Component, that I’ve attached to the camera (the player), I’m setting the speed in this function, which is called in update():

    void doInput(){

    fTimer += Time.deltaTime;

    if (Input.GetButtonDown(“Fire1″)){
    fTriggerTime = fTimer;
    }
    else if(Input.GetButtonUp(“Fire1″)){
    fFirePower = 200*(fTimer – fTriggerTime);

    Debug.Log(“Firepower: ” + fFirePower);

    Instantiate(projectile, transform.position, transform.rotation);
    Ballprops props = projectile.gameObject.GetComponent();
    props.speed = fFirePower;

    fFirePower = 0.0f;
    fTimer = 0.0f;
    }

    float fHoriz = Input.GetAxis(“Horizontal”) * fSpeed * Time.deltaTime;
    transform.Translate(fHoriz,0,0);

    }

    As my firepower is calculated correctly, the actual speed of the projectile is sometimes different, even when the firepower is nearly the same.
    600ish firepower, actual speed feels like 20. The ball barely pops out. 600ish firepower again, actual speed feels like 1000. Some shots are surprisingly accetable, but of course, that’s not what I want to attempt. ^^

    Where’s my mistake?

    Thanks for every helpful thought on that :)

    • I’ve read about the differences between Update() and FixedUpdate() and that AddForce should be called in FixedUpdate(). So, i’ve tried that but couldn’t notice any difference, same behavior as before.

      Log Test1:
      Firepower: 506 Result: No hit.
      Firepower: 778 Result: No hit.
      Firepower: 1021 Result: HIT.
      Firepower: 1050 Result: HIT.
      Firepower: 414 Result: HIT.

      Log Test2:
      Firepower: 287 Result: No hit.
      Firepower: 601 Result: No hit.
      Firepower: 724 Result: No hit.
      Firepower: 380 Result: HIT.
      Firepower: 343 Result: No hit.
      Firepower: 557 Result: No hit.
      Firepower: 980 Result: No hit.
      Firepower: 913 Result: HIT.
      Firepower: 745 Result: No hit.
      Firepower: 829 Result: No hit.
      Firepower: 368 Result: HIT.

      Thx for any help. :)

    Ok, I need some help I believe. I think I’ve pretty much got this figured out, but my counter is counting by 2′s. I do not understand why. This is the script for my sphere hitting the target:

    function OnCollisionEnter (myCollision : Collision) {
    if (myCollision.gameObject.name == “Target”){
    var myScript : ScoreKeeper = gameObject.Find(“Score”).GetComponent(ScoreKeeper);
    var Counter : int = 0;
    myScript.Counter++;
    }
    }

    and this is the script I have for the Gui text:

    var Counter : int = 0;

    function Update () {
    guiText.text = “Score is: “+Counter;

    }

    As far as I can tell the sphere is only hitting the target once, so please help me understand what I am doing wrong.

    • Excuse my post. Moments after I written it, I decided to debug.log just to make sure and my ball was hitting the target twice each time. It was just hitting the target twice in the blink of an eye it seems.

      • True, you’d need to destroy it straight away so that doesnt happen

    I have one minor problem that some of you guys have figured out in your games.

    Its how to make the camera stop when hitting a “world” wall so it only can move within a limited space where the game is going on?

    • you would put a collider on the camera to make it hit walls, but really you’d most likely clamp potential movement values using Mathf.Clamp – try either solution and see how it feels when you play it.

      • Thanks worked like a charm!

        Before I wrote the post I had messed around with a collider on the camera, but no matter what I did the camera would still push the wall away and make the camera change direction.

        But the Mathf.Clamp did the job perfectly. Silly me who stopped just before the B26 that explains it. :)

    Hi. Could you help me with 1 thing? I keep getting message that variable has not beed assigned…
    I’ve got this script.
    What should i change to fix it?

    var thePrefab : GameObject;

    function Update (){

    var horiz : float = Input.GetAxis(“Horizontal”);

    transform.Translate(Vector3(horiz,0,0));

    if(Input.GetButtonUp(“Jump”)){

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

    }

    • if you have a message saying variable hasn’t been assigned then its not the script its that you’ve forgotten to drag and drop something to the public member variable ‘thePrefab’ in the inspector.

    Done!!! =D more modules and challenges please!!!!!

  • Thank you for your tutorials and this interesting chellenge. First I thought it was too easy. But I had some irritating problems, because I work with c# en not with java. On thing that really bothered me was, I was creating the collision script and was waiting for a collision with the object “Ball”, but it was “Ball(Clone”.. I was stuck for a whole hour D:.

    Keep them coming ! (y)

    greets

  • Hello,

    Great website and tutorials! Because of this challenge, I was able to learnt the basics needed to make my Senior Project. I wanted to ask you a question. When I’m playing my game, for some reason, I have to hit the boxes in order. If I hit one out of order, the box behind it takes it’s place. Here is the code.

    var explosion : ParticleEmitter;

    function OnCollisionEnter (col : Collision) {
    if(col.gameObject.name == “Floor”){
    Instantiate(explosion, transform.position, transform.rotation);
    Destroy(gameObject.Find(“Cube”));
    }
    }

    The box falls and explodes when it touches the ground. If there is any more information I can provide, tell me.

    • Hi Carlos – just need to know what you mean by ‘the box behind it takes it’s place’ – not sure what you mean at the moment.

      Perhaps because you are destroying something you find called ‘Cube’ instead of the object you fire or hit? What is cube? is that what you’re firing in your game or what you’re shooting at. Is it the object the script is attached to?

      • Well, the boxes are placed in an order, and when the box, which is the object being shot at, falls on the floor, which is the object the box is suppose to touch to explode, the box made before the box being destroyed takes over the destroyed box’s position. I hope to makes sense. Would it be better if I send you a link to a video explaining the glitch?

    [...] So I tried one of the challenges. First I thought it was going to be easy but it was more challenging as I thought. So I was very happy when I finished it. Unity3dStudent.com [...]

  • Ohh… how come everyone knows how to make a First-Person Camera like in the clip ? This wasn’t mentioned in any tutorials, or am I really stupid ? T__T

    • Many people simply make use of the First Person Controller in the Unity standard assets package, so perhaps thats why you’ve seen it used in developers who are new to unity – see Assets > Import > custom package.

    Is it a good idea to make a boolean variable, that when it’s true, the boxes give points when hit, but when false they don’t, and to make a script which sets that variable to false when they are hit, so there are no problems with getting double score on one box? Sorry for my english…

    • You’re english is great! And yes that makes sense – a perfectly good way of toggling whether it has been hit already.

    Firstly congratulations on the excellent video lessons that have posted here.

    I wonder if you guys intend to put more video lessons, as well as new challenges in your site?

    It would be fantastic ;)

    • Hi Jorge, Just FYI I am busy creating a similar resource to this for Unity officially, so will be posting new content here less often.

    Okay, figured it out. I thought posting here would maximize how stupid I looked and thus lead to a solution.

  • Great challenge, i had troubles with the score… it took me some time to figure it out. I always use a class with private atributes and i access them instantiating an object in the other class and calling the methods to access that atribute… i don’t know why but i couldn’t do it on unity, so my solution was to create a static var so the other class could access it without instantiating an object, i hope this info comes in handy if someone else has the same problem.

  • Starting with the understanding of AS3 and using these tutorials I finished my first game ever. I think it answers to all the challenges.

    http://www.kongregate.com/games/TheEpicJoe/disk-challenge

Leave a Comment