Search Unity

Space Shooter Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Mar 26, 2015.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    blacktoy likes this.
  2. billc

    billc

    Joined:
    Apr 22, 2015
    Posts:
    1
    • Unity 5: Ambient light is now set differently. Please delete the default skybox from the Lighting panel and make sure it is set to "None".

    What does this mean? Could you elaborate? I am not finding a skybox. I have a Directional light, but did not find anything related.
     
  3. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    In the "Windows" option (next to File, Edit...) you can find a Ligthing option. It will open a new windows with the scene ambiant light setting. In there, you will find an option set to Skybox (I don't remember which one exactly). I guess you can change that option to none, disabling the ambiant light of your scene. I guess, I did not try it.
     
    billc likes this.
  4. Kaziarl

    Kaziarl

    Joined:
    Apr 23, 2015
    Posts:
    10
    On videos 7 and 8, when firing bolts it shows:

    Bolt(clone)
    Bolt(clone)
    etc in the Hierarchy.

    But on my project was getting:

    Bolt(clone)
    Bolt(clone)(clone)
    Bolt(clone)(clone)(clone)
    etc.

    The old code wasn't working, so I had to mess around till I figured something out. Which was this:


    public GameObject shot;
    public GameObject shotClone;
    public GameObject shotSpawn;
    public float fireRate;

    private float nextFire;

    void Update ()
    {

    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
    shotClone = Instantiate(shot) as GameObject;
    nextFire = Time.time + fireRate;
    shotClone.transform.position = shotSpawn.transform.position;

    }
    }


    Originally I had shot = Instantiate(shot) as GameObject; Which is where I think the (clone)(clone) thing was coming from. So I added the shotClone, and made it spawn shotClone instead of shot. But I was wanting to know if there was a better way of doing this.
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please learn to format your code correctly using code tags.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I would suggest that you got back and follow the steps as they are in the video. This code is working correctly. All the known issues should be published in the original post.

    One suggested bit of reading is this post on scope:
    http://forum.unity3d.com/threads/space-shooter-tutorial-q-a.313899/page-2#post-2069353

    Part of your issue is the scope in which you are declaring your variables, and how you are reusing them.
     
  7. Kaziarl

    Kaziarl

    Joined:
    Apr 23, 2015
    Posts:
    10
    I'm sorry, but that doesn't appear to be correct. When I follow the instructions in the video, as I did the first time, I get this error. Hence, the modified code.
     

    Attached Files:

  8. Nefius

    Nefius

    Joined:
    Apr 25, 2015
    Posts:
    1
    Hello i have a problem , at the ending when I restart the game it seems that I loose all directional lights, and the game is suddenly unlit.
    What is going on?
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ah - Ok.

    What I mean is don't invent work-arounds. All of the known issues related to versioning differences (4.x to 5.x) are listed in the original post. You put code in completely different places in your script. This is leading to difference in the way your project behaves.

    Like I said, if you're curious as to why some of these differences are, check out the link I sent re: scope, and note the difference in scope you have in your project.

    The main issues you need to worry about in upgrading are finding and cacheing the references to other components, no pre-made GUIText GameObject, the convex setting on the mesh collider and the change in ambient lighting in Unity 5. Other than that, you should follow the code as is.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you followed the steps in the original post about removing the skybox as an ambient light source?
     
  11. SkeptiDad

    SkeptiDad

    Joined:
    Apr 17, 2015
    Posts:
    11
    Not sure if this has been asked. Why for spawning waves did you use a coroutine with a while true loop? Why not use a timer in the update function(similar to the one for firing bolts)?
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    It's often more efficient to use a co-routine.

    With Update, you have to check the code every frame and test it.

    In a co-routine, the code it only checked when it's needed.

    The main reason to have the firing code in Update is to facilitate checking for Input.
     
  13. Gendo

    Gendo

    Joined:
    Apr 24, 2015
    Posts:
    5
    Hi, i have this code for the PlayerController script in Unity 5 but I'm unable to drag it onto the player object as it says to check for compile errors and whatnot, i don't see any error anywhere :(

    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class Boundary
    {
    public float xMin, xMax, zMin, zMax;
    }
    public class PlayerControler : MonoBehaviour
    {
    public float Speed;
    public float Tiltz;
    public float Tilty;
    public float Tiltx;
    public Boundary boundary;

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    GetComponent<Rigidbody>().velocity = movement * Speed;

    GetComponent<Rigidbody>().position = new Vector3
    (
    Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)

    );

    GetComponent<Rigidbody>().rotation = Quaternion.Euler(GetComponent<Rigidbody>().velocity.x * -Tiltz, -90 + GetComponent<Rigidbody>().velocity.x * Tilty, GetComponent<Rigidbody>().velocity.z * -Tiltx);
    }
    }
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please learn to format your code:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
    Scivreds likes this.
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The compile error will be in your console. If it is not open and docked, you can get to the console by clicking that last message in the footer of the editor (which could also be your compile error!) or by selecting the windows menu and choosing console.

    This will tell you what the error is and where to find it.

    If you are still having trouble, please copy the error from the lower field in the console while the error is selected in the upper part, and paste it here for more help.
     
  16. Gendo

    Gendo

    Joined:
    Apr 24, 2015
    Posts:
    5
    Sorry just noticed the class "PlayerController" was mispelled in the code i've copied from the Youtube comments, thanks for your time!
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  18. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    Hi Adam,
    I build this project (space shooter) as an Android project.

    Unfortunately, there is a problem with the laser bolts. They aren't displayed correctly, if you shot.

    Is this a bug or a script error?

    Thanks and regards
     
  19. Obsidienne

    Obsidienne

    Joined:
    Apr 25, 2015
    Posts:
    2
    Hi Adam,

    I reached the "Spawning waves" section and everything works fine. The only issue I'm encountering is the asteroids (Asteroid(Clone)) that don't get destroyed by the player, don't get destroyed by the boundary, like the bolts do. I double checked all my scripts and I can't figure out what's causing this. Any pointers?

    Thanks.

     
  20. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    Are your bolt parent gameobject and your boundary trigger ?
     
  21. Gomisan

    Gomisan

    Joined:
    Apr 27, 2015
    Posts:
    5
    I've taken this project a step further and added in a second enemy using the enemy spaceship models. This is working fine, but I'd like to have them move o the horizontal axis as they head down the screen towards you. Either in a back and forwards 'sine wave' pattern, or slowly trying to align themselves with the player.

    Is it within the scope of this thread to give some pointers as to how to do that?
     
  22. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    My bolt are a parent gameobject and also my my boundary is triggered.

    As a Standalone built there is no problems with the bolt. But if you built it as an android project, the bolt disappears after 1-2 secondes, when shooting.

    Could somebody try this out. I don't think that problem appears only by me.
     

    Attached Files:

    Last edited: May 5, 2015
  23. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    Sorry, I was talking to Obsidienne.

    But about you issue, did you try with a new gameobject instead of your bolt ? You make a cube, add the bolt script and see how it react.
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I don't know. I'll need more information...

    Do these display correctly in the editor? Can you build a stand-alone or web-player and does it display well there? On the device, what's the problem? "They aren't displayed correctly..." Are they the wrong color? Wrong orientation?

    Please enlighten...
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You can find this in the "Done" part of the project. There is an example on how to implement the enemies.
     
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Let us know how you get on, once you've checked out what @Zamas has suggested.

    You can also compare your scene with the "Done" scene.

    Lastly, don't forget to check both scripts and the editor setup, as both need to be correct.
     
  27. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    I can build a stand-alone and web-player without any problems.

    Here is a Video with the problem in the android built.
    https://vid.me/9lVC
    At the beginning and end the bolts are disappearing.

    In the editor everything seems ok. A stand-alone or web-player built works also fine. This problem appears only in the android built.
     
  28. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    Really strange. It's looks like they hit something and vanish beacause of that. I thought at first that it was because your hazard didn't disappear and the gameobject was still triggering the destroybycontact script, but it does not look like its the case.

    Can you check your hierarchy and make sure your gameobject are destroyed ?
     
  29. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    I checked it. Everything is like in the tutorial. :(

    A stand-alone or web-player built works fine. There must be another problem.

    Any further ideas?
     
    Last edited: May 5, 2015
  30. Obsidienne

    Obsidienne

    Joined:
    Apr 25, 2015
    Posts:
    2
    Thank you Adam and Zamas, it was a code 18. I made all the changes on the child asteroid rather than the parent game object.
     
  31. Gomisan

    Gomisan

    Joined:
    Apr 27, 2015
    Posts:
    5
    Thanks for your answer, but what do you mean by the 'Done' part? The last 'chapter' of the tutorial here ( http://unity3d.com/learn/tutorials/projects/space-shooter/building-the-game )doesn't have any further info as far as I can tell. Am I missing something obvious?
     
  32. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    In the asset package you've downloaded for this project you will fid a "done" folder which contains the whole project done and ready to launch. You can compare your element with the one in this folder.
     
  33. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    @Zamas
    Can you try it also on your PC please. Maybe this problem (bolts disappear) occurs only on my computer?!

    I'm using Unity 5.0.1f
     
    Last edited: May 6, 2015
  34. Gomisan

    Gomisan

    Joined:
    Apr 27, 2015
    Posts:
    5
    Well I said it might be something obvious! That code looks like an interesting variation and might do well for my 3rd enemy type.
     
  35. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    I'll try later. I'm at work currently. Do you need to load it on your android phone or can it be played on PC directly ?

    Also, did you try with the done project ?
     
  36. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    I have done it with the done project. You can download and install "bluestacks". It's an android emulator on pc and Freeware.
     
  37. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    But: When deployed on an android device, did the "done" project work correctly? Or not?
     
  38. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    I have deployed the done project on an android device and got the same problem. Could somebody confirm this problem on his device?

    I also tested it on win 8.1 with the done project and got the same problem.

    Maybe a bug in unity?
     
    Last edited: May 6, 2015
  39. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    @Adam Buckner
    Have you any suggestions to solve this problem? Or a workaround?
     
  40. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, your case is the first we've heard of. I don't have a solution or suggestion yet, as I'm not sure what the actual problem is. I don't have time to test this today, as I have several pressing things to deal with. Can you get a different device and try it on that one? I have tested this project on iOS, which doesn't exhibit this issue, but I have yet to try android. When I get the chance, I'll give it a test.

    If this isn't good enough, you could file a bug report, if you feel it's a unity issue.
     
  41. QuantumSpawn

    QuantumSpawn

    Joined:
    May 7, 2015
    Posts:
    1
    I am new to C# and Unity - I have followed the Space Shooter tutorial to the end. I did not encounter any significant problems until I reached the section where we display the "Game Over" and "Restart" text. I doubled checked that I have the same code as in the tutorial and as far as I can see they look the same. My problem is that when I play in test mode and my Space ship crashes into an Asteroid and my Ship is destroyed, the Game Over Message and Restart does not appear.

    I have no idea as to what can be wrong. Although I do seem to get the following compile error:

    <RI.Hid> Failed to create device file:
    1 Incorrect Function


    But I can seem to find any difference between my code and that of the Tutorial
     
  42. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
    UPDATE

    I created the same android build with Unity 4.6.5 and it works fine. :)
    The frames aren't skipped. That means, there is a bug in unity 5.0.1f1.
     
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Googling the error, this may be due to a controller plugged into the computer that may be the issue?

    RI is "Raw Input"
    Hid is "Human Interface Devices"

    From what I can see it is a harmless message that can be broadcast by the Input system in cases where it's having issues with a controller. The fact that you can see this in your build may be in error, and I believe a fix is on the way to prevent people from worrying about this message.

    This does not affect your problem with the text not appearing.

    If you have identical code, then the issue is how you've set up your GameObjects and Components.

    Double check that the items in your scene are laid out correctly according to either the steps in the video, or by comparing them to the "done" scene in the project.
     
  44. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you submit a bug report and note the regression between 4.6 and 5.x? And when you are done, can you get me that report number?
     
  45. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    In video thirteen the time delay is shorter by 3 seconds for the wave wait than the actual number i put in. E.g: if i put in 4, it will only wait 1 second. If i put in 10, it will only wait for 7 seconds. Does anyone know why this is?

    Also this is a very basic question but im a bit confused. Is the only reason we use void start instead of update for the self destruction of the meteor explosion gameobjects because start uses less resources?
     
    Last edited: May 7, 2015
  46. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
  47. DGreenwood

    DGreenwood

    Joined:
    Apr 21, 2015
    Posts:
    2
    Question,

    I am using the most recent version of unity and UnityScript for working through this tutorial and I am hitting some issues.
    The Bolt is cloning when I fire but I can't see it at all. I have been back through the tutorials a few times to check anything I may have overlooked but still can't get it to show up. When I drag bolt into the Hierarchy to see the action run it shows up for a second and disappears but no movement.

    I am a high school teacher looking to integrate unity into the school and this is my first tutorial and was hoping things would go a little better. (I have been learning JavaScript for web programming and don't really have this time to pick up C# before you suggest that)

    my code is
    mover.js
    var speed : float;

    function Start () : void {
    GetComponent.<Rigidbody>().velocity = transform.forward * speed;
    }

    PlayerControllers.js
    class Boundary
    {
    var xMin : float;
    var xMax : float;
    var zMin : float;
    var zMax : float;
    }

    var speed : float;
    var tilt : float;
    var boundary : Boundary;

    var shot : GameObject;
    var shotSpawn : Transform;
    var fireRate : float;

    private var nextFire : float;

    function Update () {
    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    //GetComponent.<AudioSource>().Play ();
    }
    }

    function FixedUpdate () {
    var moveHorizontal : float= Input.GetAxis ("Horizontal");
    var moveVertical : float= Input.GetAxis ("Vertical");

    var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
    GetComponent.<Rigidbody>().velocity = movement * speed;

    GetComponent.<Rigidbody>().position = new Vector3
    (
    Mathf.Clamp (GetComponent.<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp (GetComponent.<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    );

    GetComponent.<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent.<Rigidbody>().velocity.x * -tilt);
    }

    From the above code I have narrowed down the issue I am getting, the mover.js file isn't take effect properly I shoot it creates a clone but the clone doesn't move, nor does it show up. Any idea why the bolt is not displaying in game play?
     
    Last edited: May 11, 2015
  48. Azami1986

    Azami1986

    Joined:
    May 4, 2015
    Posts:
    130
  49. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This doesn't make sense. Can you give more detail?
     
  50. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    We are invoking destroy. We invoke destroy after a set time from the point that the explosion is created. Start is called when the explosion is created. Update is called every frame. We don't want to invoke destroy every frame. We could use update to check time and destroy after a time stamp has exceeded a lifetime value, but this is far more complicated.