Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Space Shooter Tutorial Q&A [ARCHIVED]

Discussion in 'Community Learning & Teaching' started by Valdier, Jan 12, 2014.

Thread Status:
Not open for further replies.
  1. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    I did this to track High Score ...

    In GameController.cs I added private int highScore; in top section.

    Then in void Start() I added:
    Code (Csharp):
    1. highScore = PlayerPrefs.GetInt("highScore"); //retrieve saved high score
    Then in void UpdateScore() I added the following to the bottom of it:
    Code (Csharp):
    1. if (score > highScore) {
    2.     highScore = score;
    3.     PlayerPrefs.SetInt("highScore", highScore);
    4.     PlayerPrefs.Save();
    5. }


    This tracks only one high score but you could modify to keep track of top ten pretty easily.
     
    Last edited: Mar 2, 2015
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Having separate parts of your game in separate scenes is not an illegitimate architecture. This can help you contain and organize your game states. The trade off is when swapping scenes, there will be a little overhead during tear down of the current scene and loading of the new one, as opposed to having to find a way to activate and deactivate items in the same scene. I commonly, even with simple games, have a "lobby / start" scene that is separate from the main game being played.

    PlayerPrefs is not an illegitimate place to store this data. Be aware that PlayerPrefs in not very secure, so it's more suited to data you don't mind "that much" about. Mostly, you should be aware that any persistent pro-amateur could probably hack PlayerPrefs without much effort. You can look up in the documentation where this data is saved, and it's saved in a simple readable file. This is why it's recommended that only "preferences" like screen settings, sound volume, etc. be stored here.

    The more "legitimate" way of doing this is the "serialize and deserialize" data to and from a file. This is "intermediate" scripting. There is a lesson on this in the Learn section, and there are several cheap or free helper scripts that can be downloaded from the Asset Store. Search using "Serialize" or "Serializing" (realise that this can also be spelled "Serialise"). You can also search for the data style: JSON or XML. What is happening here is there are established methods of taking data and writing it to a standard format (usually JSON or SML) and then saving it to a file (often Text), and later being able to read this file and return it back to your original data in it's original format.

    The storing of the data is somewhat the "easier" part.

    The trick to High Scores is replacing the data when you get a new high score and reshuffling the high scores into a new order to accommodate the new score. Here you want to look into sorting data.

    We add a simple start screen in the "Hat Trick Catch Game" in the live training archives. Take a look at how that is set up, if you want the start screen to be part of the game scene.

    The other option is to have a "lobby" scene which can act as a start screen where you can start your game by loading a new scene.

     
    Last edited: Mar 2, 2015
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Don't forget you can use code tags for your code so the formatting can be preserved:

    Code (CSharp):
    1. if (score > highScore) {
    2.     highScore = score;
    3.     PlayerPrefs.SetInt("highScore", highScore);
    4.     PlayerPrefs.Save();
    5. }
     
  4. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    @Adam Buckner - Edited my post and added the code tags. Thanks
     
  5. Icelandic_Boy

    Icelandic_Boy

    Joined:
    Mar 4, 2015
    Posts:
    8
    I got a problem with Part 15, Counting and Displaying Score. With the newest version of Unity, GUI is gone and I got only UI, I have followed the vid, the scripts are exactly as they are suppose to be but I cant assign the UI text/Canvas to the gamecontroller in the Score Text section to display the score.

    Also, no more asteroid waves, which I assume has to do with this as I get an error when I hit play, that the Score Text section has yet to be assigned.

    What am I suppose to do?
     
  6. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    303
    Icelandic_Boy Do you have Unity 5 or Unity 4.6? I haven't downloaded and installed Unity 5 yet, because I wanted to finish a few things with this tutorial first (more hazards, etc). If you've got Unity 4.6 I can help you with the UI. If it's Unity 5, I hope someone else has the answer :)
     
  7. Icelandic_Boy

    Icelandic_Boy

    Joined:
    Mar 4, 2015
    Posts:
    8
    Oops, sorry, forgot to mention that part. Yeah, Im working with 4.6 at the moment. Thinking of getting U5, though.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I need to annotate that video. Since 4.6 there is no short cut for GUIText game objects. The component exists, but you'll have to make your own manually. To follow the tutorial exactly, you can replace the step where it smells you to create a GUIText object from the menu with creating an empty game object and using the add component button to add a GUIText component.

    If you want to know how to use the new UI system, it is described in the live training session where we update the project for mobile.
     
  9. Icelandic_Boy

    Icelandic_Boy

    Joined:
    Mar 4, 2015
    Posts:
    8
    Now im getting error message when playing:
    NullReferenceException: Object reference not set to an instance of an object
    DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripts/DestroyByContact.cs:32)

    Also, the asteroids dont get destroyed. The explosion effect is there along with audio but the asteroids themselves just keep on coming.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Reading that error: You are getting a null reference exception... This means you are trying to use an object in the game but the reference is returning null - or nothing. It's like asking the guy in the coat check for your coat, and he can't find it. "It's not there!" There is no instance of the object you are looking for.

    In the second line, the error message tells you who wants this reference and where it is being called in your code.

    Destroy by contact is calling it on line 32 of your code.

    You'll need to look at what's being expected at that line of code and then deduce what's missing.

    This is usually something you're forgotten to drag a reference for in the inspector, or you are trying to get a new reference thru the collider reference.

    If you're really stuck, you can compare your code to the done code in the project or the sample code on the lesson page. If that's correct, and probably is, then you need to check the set up of your scene. You may have forgotten a step in setting up the game objects. Here again, you. An check the done scene, of probably, better, repeat the steps of the video and try to catch where you left the trail.
     
  11. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    @Icelandic_Boy - Somebody re-did the GUI tutorial using Unity 4.6. I am not sure where I found the link, I think possibly on an earlier page in this thread. They did a real nice walk thru on how to do the gui stuff for the tutorial using the new gui tools in 4.6
     
  12. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    303
    Sorry I didn't respond earlier. Here's the video of the new UI in the Roll-a-Ball Tutorial made with Unity 4.6. This will help you with the Space shooter as well.

     
  13. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
  14. DominicSals

    DominicSals

    Joined:
    Mar 4, 2015
    Posts:
    1
    Hi Adam, great tutorial so far I've learnt a lot considering I started 2 days ago!

    anyway at the end of your shooting tutorial video I did as you demonstrated and binded my bolt in the prefab to the shot and Spawnshot to the one in the hierarchy. Yet when I press play I can move around fine, although I cannot trigger bolts to be fired at all.

    Apart from this hiccup everything has worked great, hopefully you can help? :)

    *Edit* I fixed it no worries. Turns out there was a once space gap inbetween input.getbutton and ("Fire1"). All sorted.
     
    Last edited: Mar 4, 2015
  15. Icelandic_Boy

    Icelandic_Boy

    Joined:
    Mar 4, 2015
    Posts:
    8
    Okay, I am now confused as hell with that video. I have NO idea what I am suppose to replace from the old stuff from that video I just watched, what I am suppose to change, what I am suppose to leave alone.

    I now got
    Assets/scripts/DestroyByContact.cs(32,32): error CS1061: Type `GameController' does not contain a definition for `AddScore' and no extension method `AddScore' of type `GameController' could be found (are you missing a using directive or an assembly reference?)

    going on, no idea how to fix it.
     
  16. Icelandic_Boy

    Icelandic_Boy

    Joined:
    Mar 4, 2015
    Posts:
    8
    Okay, I just deleted the line gameController.count(scoreValue); that seemed to do the trick, was also able to attach the text to the score field in game controller.

    The game is running, the asteroids are spawning and I can destroye them. But I dont get any score from them. What am I suppose to do?
     
  17. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    303
    Hey Icelandic_Boy,

    As for the UI, ignore what the original tutorial video tells you about GUI.

    First step:
    Create -> UI -> Text

    This will create:
    - Canvas -> Text
    - Event system
    Leave the Event system as it is. Do NOT remove it. You don't need to do anything with it either.

    Let the Canvas itself be as it is.
    As for the "Score Text" Part, click on the Text under the Canvas. Click on it again after waiting a few seconds so that you can edit the name for that game object and make it Score Text.
    Next, reset the Transform.
    Now select the Game screen, so that you'll be able to see the text in your game (appearing).
    Next Click on the square above "Anchors" in the Transform component and click Top - Left. Next, adjust the X and Y position. My X and Y position are at 100, -65
    Now go to the Text Script Component and type "Score Text" in the field. Change the font to how you want it to look. You might want to change the color as this is usually a blackish Grey color and with the black-green background of the game, this doesn't work.

    Do the same for The Restart text and the Game Over text (Position them where you want them).

    Now, open your Game Controller Script. I'm assuming you're using CSharp (C#), btw.
    Under
    Code (CSharp):
    1. using System.Collections;
    paste:
    Code (CSharp):
    1. using UnityEngine.UI;
    Then:
    Before Void Start, paste:
    Code (CSharp):
    1.     public Text scoreText;
    2.     public Text restartText;
    3.     public Text gameOverText;
    4.  
    In Void Start, paste:
    Code (CSharp):
    1.         gameOverText.text = "";
    2.         restartText.text = "";
    3.  
    In IEnumerator SpawnWaves, if statement Game Over paste:
    Code (CSharp):
    1. restartText.text = "Press 'R' for restart";
    In void UpdateScore, paste:
    Code (CSharp):
    1.     {
    2.         scoreText.text = "Score: " + score;
    3.     }
    4.  
    In public void GameOver (), paste:
    Code (CSharp):
    1.     {
    2.         gameOverText.text = "Game Over!";
    3.         gameOver = true;
    4.     }
    5.  
    I think, out of the top of my head, this should be it.

    Good luck with the UI.

    As for the asteroids not giving points, have you set the score value in the gameobject/prefab? I know I forgot it the first time :)
     
    OboShape likes this.
  18. Icelandic_Boy

    Icelandic_Boy

    Joined:
    Mar 4, 2015
    Posts:
    8
    Hey Sheriziya, thanks a lot for the help there. It took me a while to do this, had to watch some of the videos again, especially the last ones and look over the scripts, but I now got it. I got the score text working, game over text, restart, the asteroids come and explode and I get score.

    Thanks a lot for the help!
     
  19. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    303
    You're welcome, Icelandic_Boy We're all here to help each other, right? :)

    And good to know you get the score now!
     
  20. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    303
    Hey all,

    Don't want to double post, but since this is something I just "discovered" I thought I'd better make another post.

    I just made a build of my Space Shooter game. The build for the computer was perfect. However, when I wanted to build it for the webplayer, I could select a folder, but I couldn't give a name to the build. Automatically I was given the names webplayer.html and webplayer.unity3d. I'm pretty sure that wasn't meant to happen.

    Since this was for two games (this game and the Roll-a-Ball) I had build in Unity 4.6 I decided to wait with downloading and installing Unity 5 until I had these completely finished.

    My version of Unity is 4.6.2f1

    Thought I'd better let you know.

    I will now not deploy these two to the web, since they've got the same name and that won't work with my Wordpress set up. I'll simply give the download links for both games.

    I hope we'll be able to give names ourselves again (like the tutorial says we can) in Unity 5. :)
     
  21. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Hi aye Sheriziya,

    just from my observations, if you build for web to a set folder, it takes the name of that folder for your build filenames.

    so you could create a folder called mySpaceShooter_WEB, and this should create your files like mySpaceShooter_WEB.html and mySpaceShooter_WEB.unity3d when you build.
     
  22. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    303
    Heya Obo, Yeah, that was my solution too, although it's in essence a workaround. I think it would be easier to name the build itself, instead of naming the folder. I have slept on it and decided I might as well name the folder appropriately and deploy the games to the web (in other words to my portfolio :D ).

    How does this work in Unity 5? Do we have to name the folder there too? Or can we name the build itself? If we have to name the folder appropriately in Unity 5, like we have to do now with 4.6 then this should be noted in the tutorial :)

    Maybe Adam Buckner can make a note with this last chapter of the tutorial, since the video obviously isn't up to date anymore? :)

    Thanks for the help, OboShape :)
     
  23. RickSaada1

    RickSaada1

    Joined:
    Mar 9, 2015
    Posts:
    15
    So when I try and build/run this tutorial in Unity 5, I get an error because the mesh for the mesh collider isn't convex. Is there an update to the model? I tried redownloading the assets, but it doesn't appear to have been updated.

    Thanks,

    Rick
     
  24. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon RickSaada1,
    just tried this myself to see if I could suggest anything.

    If you select your player in the editor heirarchy. then in the inspector if you scroll down to 'Mesh Collider' putting a tick in the Convex option then try playing the scene there removed this error for me.

    hope it helps,
    Obo
     
  25. Dielo

    Dielo

    Joined:
    Mar 11, 2015
    Posts:
    1
    I am a complete novice and have decided to learn a new skill in life.
    Sorry if I am double posting but at my minds end
    I am currently on part 6 of the Space Shooter tutorial Creating Shots and I have created the C# script and was not getting the speed value when I went back into Unity Mover(script).

    I have checked the script several time and even checked on the Done_mover script and it shows I am doing it correctly
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Done_Mover : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     void Start ()
    9.     {
    10.         GetComponent<Rigidbody>().velocity = transform.forward * speed;
    11.     }
    12. }
    I am getting an 2 errors on line 10 saying
    Shooter 6 error.jpg
    any help is appreciated
     
  26. the_5th_ninja_turtle

    the_5th_ninja_turtle

    Joined:
    Mar 11, 2015
    Posts:
    1
    I have a question regarding Chapter 8: Shooting Shots. I am using Unity 5 Free Version.

    I can't seem to get my shots to shoot from my Ship. I have review the video countless of times and still can't find the solution. I have also scanned the done_playerController Script and It looks like I followed it to a T but still can't get results. Here's my code:
    Code (CSharp):
    1. usingUnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class BoundaryTut
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8.  
    9. }
    10.  
    11. public class PlayerController : MonoBehaviour {
    12.  
    13.     public float speed;
    14.     public float tilt;
    15.     public BoundaryTut boundary;
    16.  
    17.     public GameObject shot;
    18.     public Transform shotSpawn;
    19.  
    20.     public float fireRate;
    21.     private float nextFire;
    22.  
    23.     void Update ()
    24.     {
    25.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    26.         {
    27.             nextFire = Time.time + fireRate;
    28.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    29.         }
    30.  
    31.     }
    32.  
    33.     void FixedUpdate()
    34.     {
    35.         float moveHorizontal = Input.GetAxis ("Horizontal");
    36.         float moveVertical = Input.GetAxis ("Vertical");
    37.  
    38.         Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);
    39.         GetComponent<Rigidbody>().velocity = movement * speed;
    40.  
    41.         GetComponent<Rigidbody>().position = new Vector3 (
    42.             Mathf.Clamp (GetComponent<Rigidbody>().position.x , boundary.xMin, boundary.xMax),
    43.             0.0f,
    44.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    45.         );
    46.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody> ().velocity.x * -tilt);
    47.     }
    48. }
    49.  
     
  27. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    As Oboshape pointed out, simply selecting "convex" on your mesh collider should solve that issue:
     
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Dielo: I'm not sure what to suggest. I have the "Done_Main" scene working and the only change I needed to make was to click the "convex" check box in the mesh collider.

    I have that same script "Done Mover" working in the scene, and get no errors.

    Is there any more information?

    Can you load the "Done_Main" scene and run it?

    [EDIT]

    What version of Unity are you running? This should work fine in this configuration for Unity 5. For Unity 4, the line should work as written above, but was originally written as:

    Code (csharp):
    1. rigidbody.velocity = transform.forward * speed;
     
  29. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There is no functional difference between versions now.

    With the exception of the skin, splash screen and services subscriptions, the versions are functionally identical.
     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664

    What is the behaviour that you are getting?

    Are you getting any errors? Do the shots appear in the hierarchy at run-time? Have you changed any of your Input names in the Input Manager?

    The only things I can suggest if your code if fine, is to look at your hierarchy and component setup to make sure that you've assembled and referenced your GameObjects correctly in the scene.
     
  31. ryan42

    ryan42

    Joined:
    Mar 15, 2015
    Posts:
    2
    Hi Adam,

    I just finished up the "Shooting Shots" section and I've been having a minor problem since the previous, part 7.
    The animation of my shots is very laggy and doesn't look like the smooth animation in the videos in the tutorial.

    Any idea on the possible cause of this? my Mover.cs script is exactly as listed below the video in part 7.

    Thanks,
    Ryan
     
  32. yosimitysam

    yosimitysam

    Joined:
    Mar 16, 2015
    Posts:
    3
    I have just got the movement script to work almost. The done file is very different than the tutorial. But I now have a mirror spaceship about -4 in the z axis. movement horizontal works. vertical is limited due to boundaries. code follows.using UnityEngine;
    using System.Collections;

    Code (csharp):
    1.  
    2.     [System.Serializable]
    3.     public class Boundary
    4.     {
    5.         public float xMin,xMax,zMin,zMax;
    6.     }
    7.  
    8.     public class PlayerController : MonoBehaviour
    9.     {
    10.         public float speed;
    11.         public float tilt;
    12.         public Boundary boundary;
    13.  
    14.         void FixedUpdate ()
    15.         {
    16.             float moveHorizontal= Input.GetAxis ("Horizontal");
    17.             float moveVertical = Input.GetAxis ("Vertical");
    18.  
    19.             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    20.             GetComponent<Rigidbody>().velocity = movement*speed;
    21.             GetComponent<Rigidbody>().position = new Vector3
    22.                 (
    23.                 Mathf.Clamp
    24.                 (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    25.                 0.0f,
    26.                 Mathf.Clamp
    27.                 (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    28.                     );
    29.             GetComponent<Rigidbody>().rotation= Quaternion.Euler (0.0f,0.0f,  GetComponent<Rigidbody>().velocity.x *-tilt);
    30.         }
    31.    }
    [edit: moderator; code tags, code formatting]
     
    Last edited by a moderator: Mar 16, 2015
  33. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The first thing you should do is visit this page, and see how to use "code tags" properly to format your code!
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
    (^_^)
     
  34. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm not sure what the issue you are having is, exactly...

    Are you saying you have two player ships in the game?

    If that is the issue, these are not placed by code, but placed in the hierarchy view.

    If you do have two, simply delete one from the hierarchy.
     
  35. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    From your description, I'm finding it hard to guess at the cause.

    Have you tried this deployed on a device?

    Don't forget that scripting is only part of the puzzle.

    Double check how the Scripts, GameObjects and Components are assembled in the scene.

    To rule out your machine, try running the "Done" scene and see if there are any issues with that one.
     
  36. yosimitysam

    yosimitysam

    Joined:
    Mar 16, 2015
    Posts:
    3
    Thank you for the link on how to display the code.

    It appears that there are two ships; I double checked the hierarchy and there is only one. on testing a little the ship are blinking and their transform is the input for zMin and Zmax.
    What I think is happening is that in one update the ship is positioned at zMin, and in the next update it is at zMax; at .02 seconds this makes it look like 2 ships.
    I do not know why this is occurring.
     
  37. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I would suggest looking at the setup in the inspector.

    Double check all the values placed in the component's fields are correct.

    If it's still an issue, then compare all the items with the "done" scene.

    I'm not seeing anything overtly wrong with the code without comparing it to the code in the project.

    This means it's probably something not related to code.
     
    OboShape likes this.
  38. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening yosimitysam ,
    ive been trying to replicate this for the last hour or so staring at lines of code to compare as it was bugging me:), finally managed to get it replicated.

    have a look at your boundary sizes in the inspector on your playerController script, ensure that zMin is a negative number, and zMax is a positive number.
    if they are the other way around, you end up with your player being at alternate locations each frame.

    It ends up throwing a hissy fit, as your minimumZ value is greater than the maximumZ so it can never be inbetween so it constantly alternates to try and resolve, but it can never happen.


    see if that resolves it please.

    phew, time for a coffee lol

    [just seen your post Adam to check component values after i found what is was lol]
     
    Last edited: Mar 16, 2015
  39. yosimitysam

    yosimitysam

    Joined:
    Mar 16, 2015
    Posts:
    3
    Rechecked the values and Yes had them inverse. Put them in the correct order and now it is working. Interesting mistake. Not all problems are coding errors.
     
  40. ryan42

    ryan42

    Joined:
    Mar 15, 2015
    Posts:
    2

    Well I fixed it, I obviously messed something up in the Bolt prefab or the VFX.
    Something about mine was causing the lag.
    I copied the Done_Bolt prefab in and relinked to the player as the shot and my bolts are firing fast now.

    I wish I knew exactly what I did to mess up my bolt but oh well! Thanks for the help!
     
  41. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Woo7! Don't forget that Unity is not just code. I don't find errors for a long time when I doubt my code over and over again, and it turns out the code it fine, but the implementation in the scene is flawed.
     
  42. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    150
    Hi, great tutorial! I finished the basic set. Where can I find more lessons like adding scrolling background, enemies or starfield as announced in Introduction? Thanks.
     
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Those are "stretch goals" that the student can try themselves (at this point).

    You can find them in the "done" folder.

    Open the "Done_Main" scene, and look at how they are implemented in the final version.
     
  44. ImaLuckyMan

    ImaLuckyMan

    Joined:
    Mar 18, 2015
    Posts:
    1
    Gentlemen,
    I am brand new to Unity and programming in general, so I apologize if I am doing something wrong. I am working my way through the tutorials step by step and I love that you have taken the time to put something together so in depth. I am having trouble on two of the tutorials with Unity 5 and have been able to work past most of my compile errors with out problem. Even though there are no errors and shown and the game thinks it is working correctly... I have an issue On both the Space Shooter, and Survival Shooter Beginer tutorials and I think I have been able to narrow the issue to Quaternion. On my space shooter I believe the Quaternion portion of the script is to fixate the rotation of the asteroid upon its creation... but ever since I incorporated the script and run it, no asteroids appear unless I physically place them on the screen. If I place them, they work fine... roll down the screen and are destroyed as they are suppose to... but they are not being randomly created by the game controller as they are suppose to. I am running into a similar issue in the Survival tutorial, I believe Quaternion is suppose to change the look angle or direction of the player manipulated by the direction of the mouse, and my player moves just fine N,S,E,W, but does not turn. During my research in other forums it looks as other people are having similar issues, and I was just wondering if anyone has found an answer, maybe there is a code snippet that is different in Unity 5 than it was in Unity 4 like that of Rigidbody where now you have to format it like GetComponent<Rigidbody> is there something like this for Quaternion?
     
  45. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There is probably some other, tho' possibly related, issue - but probably not Quaternion themselves.

    -

    Quaternions:

    A Quaternion, is a way of representing a rotation.

    A Quaternion is rather complicated - it sort of operates in 4 dimensions.

    Now, a rotation can also be represented by 3 numbers (or 3 floating point values), one value for each of the x, y and z angles to define a full rotation in 3D space. We can hold all of these 3 values in a data structure called a Vector3.

    There are problems with Vector3's however when dealing with rotations. They suffer from an issue called gimbal lock (this is easy to google), so the solution for dealing with rotations is to use a Quaternion.

    Now, most of the time we don't need to deal with Quaternions directly. We can use our 3 floating point values held in a Vector3 (the inspector uses this when we see a GameObject's rotation is defined by x, y & z), and Unity converts between the Quaternion and Vector3's as necessary. When we need to convert between these two representations of a rotation, we can use a thing called Euler Angles (pronounced "oiler"). Take this line of code for example:

    Code (CSharp):
    1. transform.rotation.eulerAngles = new Vector3(0, 30, 0);
    transform.rotation is a Quaternion, but we can set it using a Vector3 as long as we do so using Euler Angles.

    http://docs.unity3d.com/ScriptReference/Quaternion.html
    http://docs.unity3d.com/ScriptReference/Quaternion-eulerAngles.html

    -

    Your issues:

    It sounds like you've been doing your research, but you may not have enough experience to see what the issue is.

    First some bits of generic advice:
    • Watch the videos again and check each step in detail, as it's easy to miss small things.
    • Pay attention to the steps being taken in the editor, as our games are made not only by the code that we wrote, but the way we assemble them in the he scene: your script may be correct, but the items in the scene could be confused.
    • If you really get stuck, look at the "done" material in the project. There will be a finished scene and finished scripts to look at.
    • The completed scripts should also be available on the page associated with that episode, so you can copy the code there for comparison as well.
    My advice is to not just use the "done" code, but to compare it to your own code to see where you went off the track.

    If your asteroids at not appearing, then it's probably something to do with the "instantiate" part of the game controller. Check to make sure the code is written correctly (see above notes), and check in the editor that all of the prefabs are correct and properly associated with the script component.

    Also, are you getting an errors in your console at runtime? The most recent should show up in the footer in end.
     
  46. DrReaper

    DrReaper

    Joined:
    Mar 16, 2015
    Posts:
    12
    Check your function is working. I forgot to capitalize one letter and though all the code was correct it never processed any of it.
     
  47. DrReaper

    DrReaper

    Joined:
    Mar 16, 2015
    Posts:
    12
    Attempting Space Shooter using Unity 5 and getting a few errors on Chapter 12 "Game Controller". Here is my Code

    using UnityEngine;
    using System.Collections;

    public class GamerController : MonoBehaviour
    {
    public GameObject hazard;
    public Vector3 spawnValues;

    void Start ()
    {
    SpawnWaves ();
    }

    Vector3 spawnPosition = new Vector3 (Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    Quaternion spawnRotation = Quaternion.identity;

    void SpawnWaves ()
    {
    Instantiate (hazard, spawnPosition, spawnRotation);
    }

    }

    Here is the Unity 5 console errors list

    Assets/Scripts/GamerController.cs(14,60): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `GamerController.spawnValues'
    Assets/Scripts/GamerController.cs(14,75): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `GamerController.spawnValues'
    Assets/Scripts/GamerController.cs(14,53): error CS1502: The best overloaded method match for `UnityEngine.Random.Range(float, float)' has some invalid arguments
    Assets/Scripts/GamerController.cs(14,53): error CS1503: Argument `#1' cannot convert `object' expression to type `float'
    Assets/Scripts/GamerController.cs(14,91): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `GamerController.spawnValues'
    Assets/Scripts/GamerController.cs(14,106): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `GamerController.spawnValues'
    Assets/Scripts/GamerController.cs(14,120): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments
    Assets/Scripts/GamerController.cs(14,120): error CS1503: Argument `#1' cannot convert `object' expression to type `float'
     
  48. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The first thing you should do is visit this page, and see how to use "code tags" properly to format your code!
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
    (^_^)
     
  49. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Why are these two lines not contained within a function?
    Code (csharp):
    1. Vector3 spawnPosition = new Vector3 (Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    2. Quaternion spawnRotation = Quaternion.identity;
    They should belong to a function, rather than the class itself.

    Essentially, you need to set some of these values (the public ones) in the inspector, but they are being referenced by these two variables which are declared as part of the class: Vector3 spawnPosition and Quaternion spawnRotation, so the "field initializer" is getting confused trying to initialize spawnPosition and Quaternion spawnRotation but it can't.
     
  50. DrReaper

    DrReaper

    Joined:
    Mar 16, 2015
    Posts:
    12
    Thanks Adam. I put the lines inside the SpawnWaves function and everything is working now. I must have looked at it for an hour and a half and didn't see it.
     
Thread Status:
Not open for further replies.