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.
  2. Dismiss Notice

Space Shooter Tutorial Q&A

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

  1. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    What happens when you drag the Bolt prefab into the heirarchy? does it behave like it should?
     
    Last edited: Oct 25, 2015
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @rckkk,

    Looking at the screenshot, was that dragging the GameManager instance from the heirarchy as in the video? looks like you may have dragged the script itself by accident.

    try redragging the GameManager from scene heirarchy to the button onclick slot, see how that goes.
     
    Last edited: Oct 25, 2015
  3. rckkk

    rckkk

    Joined:
    Oct 8, 2015
    Posts:
    6
    I would never think it was so simple, from now it is working, thank you so much mate ! @OboShape
     
    OboShape likes this.
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please learn to use code tag properly?

    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    @winxalex thanks for your input! A lot of what you describe is just the learning process. If these people make a game and sell only 400 copies and then it dies on level 50 because of a memory leak, that's where you see what they're made out of. Do they give up? Do they move to another engine? (And if they did, they would probably find the exact same problems there…) Or do they pick up their tools, dust themselves off and either fix the problem - or create a new game that doesn't have it in the first place?

    It's very hard to teach somebody about how to loosely couple a project until they understand what coupling is in the first place.

    I would presume, from your approach to the subject, that you are either a trained coder or someone who has self trained in coding first and then has come to games. You need to imagine these people who we are trying to teach: They have none of this understanding and experience. Also, the main point to what these people are trying to learn is how to make a game not how to code.

    This is a very difficult and careful distinction.

    Obviously, with Unity, you need to know how to code to be able to make a game. There are, however, lots of other disciplines that are needed to make a game as well as coding. These are disciplines and experiences that you do not need when creating the average application. The type of people who come to us and want to learn how to make a game have far more varied backgrounds than the type of people who would be coming to learn how to code or program an application. Many of the people coming to Unity will not only have no coding experience, they won't have the mind set of a coder. We need to teach them as much, if not more, than the person who has a background in code.

    So, our task is divided with multiple objectives.

    One is to simply show off how the engine works.

    We can do that with more simple examples, assuming that experienced and trained coders, such as yourself, will be able to understand the framework and more efficiently put together a project on their own on that follows flexible design patterns.

    Another of our many objectives is to be able to show people with absolutely no coding experience, someone who doesn't know the difference between a variable and a function, how to get up and running fast with Unity

    This way these people have the greatest chance of continuing with games, regardless of whether they use Unity or not. We have to make the assumption that these people are made of stuff that strong enough that they can learn both how to make a game and how to code at the same time. These people have to learn a huge amount in a small amount of time.

    To do this we have to teach people in the very incremental manner. We have to assume that the people that we are teaching are going to be smart enough, strong enough, and have the endurance to start small and start simple – perhaps with tightly coupled code and inefficient design patterns – and as they progress in their learning and in their experience, to learn more code, more flexible patterns and better architecture and design in their games.

    I also think it's important that we divorce our ego from this conversation.

    It's very easy to look at something that we know very well, and we are proud of knowing, and when we see it being applied in a manner different to what we expect, to make the assumption that the other people are wrong and get arrogant about the subject. Like any topic that has a philosophical nature, there are many sides and facets to how to teach in general and how to teach Unity specifically. Creating games is far more complicated than simply creating a running application. There are many more, and more complex, issues working around the business of games and games design than ever approach the business of pure coding and application design. The markets and timescales are completely different. Often, when dealing with games, there are many more compromises that need to be made for the sake of speed, frame rate, efficiency, marketing, and delivery schedule for a game than for any common coded application. These compromises can frustrate someone who is interested in only the pure coding architecture of a project.
     
    SicraS and OboShape like this.
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Could it be that you are not using an f to indicate "float"?
    Code (CSharp):
    1. Vector3 myVector3 = new Vector3 (0.0f, 0.0f, rb.velocity.x * -tilt);
    Note the "f" after the 0.0
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The stuttering in the movement is so,entices caused by two rigidbodies. Check that you don't have a secondrigidbody by accident on the artwork.
     
    g4mdev likes this.
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Glad you got it working.

    Thanks oboshape!
     
  9. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
    I agree to some point. My critics are bit harsh(I don't suck milk), but think I'm not heater, cos I could take hat down for auto character bone mapping or animation view/mode, but I free to say that (I would say sucks but let say) I've low opinion on Input Manager, Asset store submission process and tools, Unity official tutorials... That is my point of view, might have been wrong. But when you see 1000 feedback votes and 6 years not change, then something is wrong.
    Critics can trigger some decision maker ego or even better someone way of thinking so things get change towards better. Or we can suck milk building your ego how good and great you Unity are.
    Today's most of top engines are built not thinking of architectural design. Why? Let say cos "sake of speed, frame rate, efficiency, marketing, and delivery schedule".... Ok that might have influence but I think that is cos whole generation "old school" developers who built great games, and they didn't know of architecture design(they weren't invented) and won't like to know cos of their "ego" greatness. I'm old enough to remember where there was no MVC,Service locators, Singletones, no Linq, no Observables, nunit,.....framework but I was building games, but
    let we divorce our ego from everthing and accept that C# progress, pattern progress and we need to accept that cos benefits are great(simple more money and more time to be invested on creative part that make the game good game).
     
  10. Antonios-Stergiou

    Antonios-Stergiou

    Joined:
    Oct 27, 2015
    Posts:
    4
    hi I love the tutorial with the space shooter, I don't know much about coding though I have managed to figure some things out for my game looking up Playercontroller.PNG Playercontroller.PNG Playercontroller.PNG Playercontroller.PNG online. I am stuck at the moment as for someone with coding knowledge it must be easy but for me I can't seem to get it right. I am trying to make a weapons shotspawn unlock with every time I reach say 100 or 200 points score.
    This is the script and I know part of it is wrong I just can't figure it out.


    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class Done_Boundary
    {
    public float xMin, xMax, zMin, zMax;
    }

    public class Done_PlayerController : MonoBehaviour
    {
    public float speed;
    public float tilt;
    public Done_Boundary boundary;

    public GameObject shot;
    public Transform shotSpawn1;
    public Transform shotSpawn2;
    public Transform shotSpawn3;
    public float fireRate;

    private float nextFire;
    private Done_GameController gameController;
    private bool shot1;
    private bool shot2;
    private bool shot3;

    void Start () {
    shot1 = true;
    shot2 = false;
    shot3 = false;

    {
    }
    }

    void Update ()
    {
    if (Input.GetButton ("Fire1") && Time.time > nextFire) {
    nextFire = Time.time + fireRate;
    shot1 = Instantiate (shot, shotSpawn1.position, shotSpawn1.rotation);
    shot2 = Instantiate (shot, shotSpawn2.position, shotSpawn2.rotation);
    shot3 = Instantiate (shot, shotSpawn3.position, shotSpawn3.rotation);
    GetComponent<AudioSource> ().Play ();
    {
    }

    if ((gameController.AddScore >= 100) = shot2 = true);
    {
    if ((gameController.AddScore >= 100) = shot3 = true);
    }
    }
    }

    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 (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    }
    }
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    First: Can you please learn to use code tag properly?

    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Second: Can you post your graphics so they are not in the middle of your text?
     
  12. Antonios-Stergiou

    Antonios-Stergiou

    Joined:
    Oct 27, 2015
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Done_Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10. public class Done_PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public float tilt;
    14.     public Done_Boundary boundary;
    15.  
    16.     public GameObject shot;
    17.     public Transform shotSpawn1;
    18.     public Transform shotSpawn2;
    19.     public Transform shotSpawn3;
    20.     public float fireRate;
    21.    
    22.     private float nextFire;
    23.     private Done_GameController gameController;
    24.     private bool shot1;
    25.     private bool shot2;
    26.     private bool shot3;
    27.  
    28.     void Start () {
    29.         shot1 = true;
    30.         shot2 = false;
    31.         shot3 = false;
    32.  
    33.         {
    34.     }
    35. }
    36.  
    37.     void Update ()
    38.     {
    39.         if (Input.GetButton ("Fire1") && Time.time > nextFire) {
    40.             nextFire = Time.time + fireRate;
    41.             shot1 = Instantiate (shot, shotSpawn1.position, shotSpawn1.rotation);
    42.             shot2 = Instantiate (shot, shotSpawn2.position, shotSpawn2.rotation);
    43.             shot3 = Instantiate (shot, shotSpawn3.position, shotSpawn3.rotation);
    44.             GetComponent<AudioSource> ().Play ();
    45.             {
    46.             }
    47.                 if ((gameController.AddScore >= 100) = shot2 = true);
    48.             {
    49.                 if ((gameController.AddScore >= 100) = shot3 = true);
    50.             }
    51.         }
    52.     }
    53.        
    54.     void FixedUpdate ()
    55.     {
    56.         float moveHorizontal = Input.GetAxis ("Horizontal");
    57.         float moveVertical = Input.GetAxis ("Vertical");
    58.  
    59.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    60.         GetComponent<Rigidbody>().velocity = movement * speed;
    61.        
    62.         GetComponent<Rigidbody>().position = new Vector3
    63.         (
    64.             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    65.             0.0f,
    66.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    67.         );
    68.        
    69.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    70.     }
    71. }
    72.    
    73.  
    74.  
    75.  
     
  13. Antonios-Stergiou

    Antonios-Stergiou

    Joined:
    Oct 27, 2015
    Posts:
    4
    Sorry about that I am still learning.
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Okay I think you're misunderstanding some of the fundamental/basic concepts about coding.

    At the top of your script, you declare a number of variables. These variables are containers. Think of them as little boxes that contain data. These variables, or a little boxes, can hold either values or references. So, when you declare a public transform shotSpawn; you are creating a container that can hold a reference to a transform component. When you declare a public float speed; you are creating a container that can hold the value of speed. In your case when you create private bool shot1; you are creating a variable, or a little box, that can hold the value of something that is true or false. That little box can only contain true or false, and nothing else.

    This is important when it comes to your game logic. When you have a Boolean variable, you cannot set it to be a game object!

    So, take this line of code:
    Code (CSharp):
    1. shot1 = Instantiate (shot, shotSpawn1.position, shotSpawn1.rotation);
    Here you are trying to set shot1, which a Boolean value, to the value of the game object that you instantiate! This is not possible! I am sure this generates an error in your console.

    Now this to Boolean value is still of use! You need to use this boolean value to test whether it's true or false, and then perform your function. The most simple way of doing this is with this type of code:
    Code (CSharp):
    1. if (shot1 == true) {
    2.      Instantiate (shot, shotSpawn1.position, shotSpawn1.rotation);
    3. }
    This code checks to see if shot1 is true, and it shot1 is true, then this script will instantiate the shot.

    Remember: it is all logic.

    Once you start thinking of these little bits as logical code, then it's easy to put these scripts together.

    There is code that could be both more complex but more compact. For instance, you could make a loop that checked every spawn point in a list or array and if that spawn point was active, fired a shot from that spawn point.
     
  15. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    Hello there. I have a problem with loading a new scene into the project. I've crated second scene for boss fight and I don't know how to keep my variables (score, bool variable for powerup [true or false] and other things) so that in my boss fight scene everything will be the same.
    I've found some
    Code (csharp):
    1. DontDestroyOnLoad(transform.gameObject);
    script but i dont know how to use it :/
    should this be something like:
    Code (csharp):
    1. DontDestroyOnLoad(score);
    ??
     
  16. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    TBH, this is a bit beyond the scope of supporting this tutorial.

    I'll give a couple of suggestions, but to dive deeply into this, you may have to do this in another thread.

    First, read up on DontDestroyOnLoad:
    http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

    For this to work, note the signature:
    public static void DontDestroyOnLoad(Object target);

    You need an object, usually a GameObject, not a variable.

    You can try to take a GameController GameObject with score on it and save that.

    It can very quickly become complicated, however... you will need to also carry over the UI as well, or find a way to rebuild the score display in the new level.

    One way to do this is to only save the data, and have some way to reload the data in the new scene when it starts, and have the displays hooked up and ready to go locally.

    One example of this would be to use PlayerPrefs (but be aware it's very insecure when dealing with a commercial game). You can save the data to player prefs just before you call "load level" and then you can get these values back from player prefs when the new level starts.

    If you want to get more complicated, you can try to save this data to a file. One example of this can be found here:
    http://www.sitepoint.com/saving-data-between-scenes-in-unity/

    This is by all means not the only way to do it, but it can be that start of your search.
     
  17. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    Ok I've managed to do this
    Just simple new script with
    Code (csharp):
    1.  
    2. void Awake () {
    3.         DontDestroyOnLoad(transform.gameObject);
    4.     }
    5.  
    And I've putted it into everything I wanted to save (player, hud, gamecontroller)
    And it works like a charm :D

    But I've got new problem now with this script
    I'm loading my scenes with
    Code (csharp):
    1. Application.LoadLevel ("boss");
    2.  
    3. Application.LoadLevel ("main");
    etc.
    and whenever I try to load main scene on restart (after defeating my boss player gets "Victory screen" and can press R for loading main scene again)
    I get 2 player ships (which is obvious cuz Player has that dontdestroy script)

    And here is my question:
    Is there an inverse script for DontDestroyOnLoad (like DestroyOnLoad or ForceDestroy)? So I can place it in restart function?
    Or do you have any idea how to destroy my whole data on restart (without quiting the game)
    Or how can I load "completely new instance of game" (like after quiting) [different function than Application.LoadLevel]
    Or is there anything like
    Code (csharp):
    1. If currentlevelname == "boss" then (do something)
     
    Last edited: Oct 28, 2015
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, you've got some of the correct ideas.

    Some notes: the Transform has an automatic reference to the current GameObject. You've found that with transform.gameObject. In most cases you can access the component's GameObject directly with simply gameObject. This means you can use:
    Code (csharp):
    1.  
    2. void Awake () {
    3.         DontDestroyOnLoad(gameObject);
    4.     }
    5.  
    You can get into a singleton pattern to do some of what you want, here. (Be aware that many people find the singleton pattern to be overused. If you want more details, search for "singleton" or "singleton pattern".)

    What this essentially does is on initialization check to see if an object of this type already exists, and if it does, deletes itself. Many people make GameControllers that do just this. Every scene has a GameController in it, and they also have a "DontDestroyOnLoad" configured. When it initializes, it checks to see if you have one or not, and then destroys itself if there is. This means you can start any level, and there will be a GameController, but if one has be brought over from the previous scene, it will destroy itself.

    Now, frankly, I usually just move the data across the scenes (as I mentioned above) using PlayerPrefs or a serialized file.

    If you want to destroy something, simply use Destroy(). You can look that up in the docs, and we talk about it in the Space Shooter lessons.

    Let me know if this is not enough to go on.
     
  19. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    Yeah I just delete all the objects after R
    Code (csharp):
    1. Destroy (GameObject.FindGameObjectWithTag("GameController"));
    etc.
    Dunno if this is the best method, but it works for me.

    I'm almost done with the project, now I have a problem with my boss health. It works fine when i tested it in boss scene, but after all that scene jumping now my boss gets 1 shoted (and sometimes I can take him to like 50% hp and he dissapears). I kinda dont know how to debug this. I supuse DestroyByContact is causing this, but I can't find any typo there

    btw.

    Code (csharp):
    1.  void Start ()
    2. {
    3. if (gameController.bossfight == true)
    4.         {
    5.             GameObject bossHealthObject = GameObject.FindWithTag ("Boss");
    6.             if (bossHealthObject != null)
    7.             {
    8.                 bossHealth = bossHealthObject.GetComponent <BossHealth>();
    9.             }
    10.             if (bossHealth == null)
    11.             {
    12.                 Debug.Log ("Cannot find 'bossHealth' script from dbc");
    13.             }
    14.         }
    15. }
    16.  
    that part "if (gameController.bossfight == true)"
    Is this going to work? Or is void Start checking this only once at the start and I have to put this in void Update()?


    #edit
    I did lots of debugging (debug.log in every possible trigger) and I found nothing XD boss was just dissapearing from the game at random, so I made an exception in boundry like
    Code (csharp):
    1.  
    2. void OnTriggerExit(Collider other)
    3.         {
    4.             if(other.gameObject.name != "Boss")
    5.             {
    6.             Destroy (other.gameObject);
    7.             }
    8.         }
    and it seems that this worked.
    I will test my game and update my post if this will come back :v
     
    Last edited: Oct 28, 2015
  20. floh-erfurth

    floh-erfurth

    Joined:
    Oct 28, 2015
    Posts:
    2
    Hi, I'm currently working on "Extending Space shooter: Enemies, more hazards, scrolling, bg...".

    Unfortunatelly this video isn't captioned, and I need this since I'm almost deaf. I'm stuck at 23 minutes where I should move duplicated background. There the gizmo (for anchor?) moved to a corner, how can I do this?

    Also I want to ask if there is a transcript for this video?

    Thank you very much!
    Floh
    PS: Unity3D-Tuts are great for newbies!!!
     
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Start is called only once when the game object is first made active.
    You can look into OnEnable. In the manual there is a list of events and when they are called during the game loop. This may help.

    If, on the other hand, you carry over your data, but not your game controller, and load the data into your game controller, you can check on start.

    This may be something you want to take to scripting,as it's getting beyond the scope of this teaching thread. If you do start a new thread, tag me, and I'll follow along.
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    It takes time for the captioning to be added. I'll double check the its in the queue. What you may be seeing is vertex snapping, where you hold down the "v" key and snap one vertex to another. (There are more details in the documentation.)
     
    floh-erfurth likes this.
  23. WhySoSeriousGuys

    WhySoSeriousGuys

    Joined:
    Oct 28, 2015
    Posts:
    18
    Is there a way to load scene like I would click "play" button (with complete reset, like quiting the game and restarting it from .exe) instead of loading it from "memory"?
    I want to do it with the easiest possible way (setting all the vars back to starting ones after pressing R is kinda workaround :/)
    I have DontDestroyOnLoad and Application.LoadLevel ("main") loads it from memory instead of loading completely new scene.

    sorry for beeing so insistent, but I'm so close to complete my game and this just bothers me :v

    #edit
    is
    http://docs.unity3d.com/ScriptReference/Application.UnloadLevel.html
    for unity 5.x?

    [you don't need to put web links inside code tags - this simply makes them unclickable]
     
    Last edited by a moderator: Oct 29, 2015
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664

    All of our current documentation is ... current.

    So, yes.
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    1) I feel that you are rushing to finish your project and may be making shortcuts you will regret later.
    2) You should solve your the issues that your game requires in the best way possible.
    3) You seem to be solving this in little unrelated bits, but not creating one complete system.
    4) This should probably be posted in scripting or support as it's beyond the scope of this thread:
    All of that being said, and - yes - you should probably start a new thread in support or scripting - and you can tag me on the thread to take a look - but:

    You should lay out all of the logic you want from start to finish in some sort of non-code form. Make a flowchart, or diagram of what you want to have happen in your game. Then work out each piece to they work as a system. You've worked out a few bits and pieces here and there - dontdestroyonload here, and loadlevel there, etc. When you've finished working out what you want done, then try to work out how all the pieces work together so make a system. This will also help when you make your scripting post - you'll be able to "show your work" and people will be able to help you more easily.
     
  26. floh-erfurth

    floh-erfurth

    Joined:
    Oct 28, 2015
    Posts:
    2
    Hi Adam
    I know that its a lot of work since the company where, I'm working, is doing captioning videos of customers. ;) But thank you very much for making videos accessible for deaf peoples like me!
    Ah, this is what I was looking and now I just finished the tutorial.

    Thank you very much!
    Floh
     
  27. aidenlgrayson

    aidenlgrayson

    Joined:
    Oct 23, 2015
    Posts:
    5
    Hello!
    I have been having some trouble with the shooting the shots aspects of the tutorial, I can shoot them fine, its just that every time all of the shots have been removed from the scene I can no longer shoot them.This also happens when destroying asteroids. I have an error saying that the GameObject was destroyed but I am still trying to access it. I am fairly new to C# and coding in general (know a bit of C++), so any help would be much obliged.

    (I have no idea where the problem is surfacing, so here are my best guesses)

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10.  
    11. public class PleaseMove : MonoBehaviour
    12. {
    13.    
    14.     public float speed;
    15.     public float tilt;
    16.     public Boundary boundary;
    17.    
    18.     public GameObject shot;
    19.     public GameObject shotSpawn;
    20.     public float fireRate;
    21.    
    22.     private float nextFire;
    23.    
    24.     void Update ()
    25.     {
    26.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    27.         {
    28.             shot = Instantiate(shot) as GameObject;
    29.             nextFire = Time.time + fireRate;
    30.             shot.transform.position = shotSpawn.transform.position;
    31.            
    32.         }
    33.     }
    34.     void FixedUpdate ()
    35.     {
    36.        
    37.         float moveHorizontal = Input.GetAxis ("Horizontal");
    38.         float moveVertical = Input.GetAxis ("Vertical");
    39.        
    40.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    41.         GetComponent<Rigidbody>().velocity = movement * speed;
    42.        
    43.         GetComponent<Rigidbody>().position = new Vector3
    44.             (
    45.                 Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    46.                 );
    47.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    48.     }
    49.    
    50. }

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoltMovement : MonoBehaviour {
    5.  
    6.     public float Speed;
    7. void Start ()
    8.  
    9.     {
    10.         GetComponent<Rigidbody>().velocity = transform.forward * Speed;
    11.  
    12.  
    13.     }
    14. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoundaryDestroy : MonoBehaviour
    5. {
    6.     void OnTriggerExit (Collider other)
    7.     {
    8.         Destroy(other.gameObject);
    9.     }
    10. }
    Thanks!
     
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you dragged the prefab from your project view into the shot slot on the PlayerController component? Or - have you dragged an instance of the shot from the scene view?
     
  29. Lahnaman

    Lahnaman

    Joined:
    Oct 30, 2015
    Posts:
    1
    Hi!
    In Space Shooter Tutorial I'm on lesson Creating Hazards. As soon as I added the script DestroyOnContact, both the Asteroid and the Player Ship disappeared when I entered Play mode. Adding the if statement to ignore the boundary layer didn't seem to work (I did remembered to assign that tag to the boundary). Even removing the DestroyOnContact script from the asteroid didn't make the problem go away. The only way I found to stop these game objects from disappearing in Play mode was to deactivate the respective objects' colliders, but then it will be impossible to complete the tutorial.

    This problem was supposed to only happen to the asteroid because its collider is in contact with the boundary. However player object isn't in contact with the boundary but it also disappears upon entering Play mode.

    What can I do?
     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, first do things like double check all the spelling and capitalisation of the tags and functions just to make sure everything is working correctly. You can also follow the debug instructions that are shown in the video where a line of code is used to find out which object is calling the Destroy function. Lastly, carefully re-watch the video and double check each step.
     
  31. rckkk

    rckkk

    Joined:
    Oct 8, 2015
    Posts:
    6
    Hello Again, i was trying to put a free joystick i've downloaded at asset store at my spaceshooter game but i make some erros on code because i don't know too much about scripting and than i have my game not working any more, so i've decided to delete everything and start at 0 point.
    and i've started again and now i'm having this error on my compiler, i'm looking for a thing to fix this but i don't know where is the mistake please help-me if possible, thanks at all.
     

    Attached Files:

  32. boylamn

    boylamn

    Joined:
    Jan 28, 2015
    Posts:
    19
    Hi, I built the game for Android and it's working very well on my tablet. But I wonder if it will work also perfectly on other devices or not.
    If not how I can make it support different screen resolutions.
     
  33. Kaotis

    Kaotis

    Joined:
    Oct 31, 2015
    Posts:
    1
    Hi, i'm new to unity but not to programming and just wondered if there is any (performance) reason why to play the weapon_audio in the PlayerController rather then to bind it with the bolt prefabe.

    Thanks.
     
  34. badloop

    badloop

    Joined:
    Oct 31, 2015
    Posts:
    1
    Hi,

    I am having the same issue with the ship sticking to the set boundaries and gradually going through them.

    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. [System.Serializable]
    6. publicclassBoundary
    7. {
    8. publicfloatxMin, xMax, zMin, zMax;
    9. }
    10. publicclassPlayerController : MonoBehaviour
    11. {
    12. privateRigidbodyrb;
    13. publicfloatspeed;
    14. publicfloattilt;
    15. publicBoundaryboundary;
    16.  
    17. voidStart ()
    18. {
    19. rb = GetComponent<Rigidbody> ();
    20. }
    21.  
    22. voidFixedUpdate ()
    23. {
    24. floatmoveHorizontal = Input.GetAxis ("Horizontal");
    25. floatmoveVertical = Input.GetAxis ("Vertical");
    26.  
    27. Vector3movement = newVector3 (moveHorizontal, 0.0f, moveVertical);
    28. rb.AddForce (movement * speed);
    29.  
    30. rb.position = newVector3
    31. (
    32. Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    33. 0.0f,
    34. Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    35. );
    36. rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    37.  
    38. }
    39.  
    40.  
    41. }
    42.  
     
    Last edited: Nov 5, 2015
  35. Esuna126

    Esuna126

    Joined:
    Oct 31, 2015
    Posts:
    1
    Hi, I'm currently working on "Extending Space shooter: Enemies, more hazards, scrolling, bg...".

    Got problem in moving the enemy ship. just by enabling the mover script, it moves. however, after adding the evasive maneuver script, it just dodges at its position.
     
  36. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please use code tags to format you code.
     
  37. sergivillar

    sergivillar

    Joined:
    Oct 17, 2015
    Posts:
    1
    Hi guys, I've a question, if I deploy the game in an iphone 6 then I have to set manually the spawn values to (-5, 5), is there some method to set the spawn values automatically despite the device?
    Thanks .
     
  38. Antonios-Stergiou

    Antonios-Stergiou

    Joined:
    Oct 27, 2015
    Posts:
    4
    Thank you so much after searching and getting a comment from my friend on to make my game easier and my being persistent and with the help you gave me with that code I got my level up system working with collection game objects using the script from the roll a ball tutorial.You opened my eyes,thank you so much man.I had unpick the shot Spawns and then I found with the script that the hierarchy of first to put the false shots and then the true else my player would dissipater


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerCollectObject : MonoBehaviour {
    6.    
    7.  
    8.     public Text countText;
    9.     public Text winText;
    10.     public GameObject shot;
    11.     public Transform shotSpawn1;
    12.     public Transform shotSpawn2;
    13.     public Transform shotSpawn3;
    14.     public Transform shotSpawn4;
    15.     public Transform shotSpawn5;
    16.     public float fireRate;
    17.     public float delay;
    18.    
    19.  
    20.     private int count;
    21.     private bool shot1;
    22.     private bool shot2;
    23.     private bool shot3;
    24.     private bool shot4;
    25.     private bool shot5;
    26.    
    27.     void Start ()
    28.     {
    29.         InvokeRepeating ("Fire", delay, fireRate);
    30.         count = 0;
    31.         SetCountText ();
    32.         winText.text = "";
    33.         shot1 = true;
    34.         shot2 = false;
    35.         shot3 = false;
    36.         shot4 = false;
    37.         shot5 = false;
    38.     }
    39.  
    40.  
    41.     void Fire ()
    42.     {
    43.         if (shot1 == true) {
    44.             Instantiate (shot, shotSpawn1.position, shotSpawn1.rotation);
    45.         }
    46.         if (shot2 == true) {
    47.             Instantiate (shot, shotSpawn2.position, shotSpawn2.rotation);
    48.         }
    49.         if (shot3 == true) {
    50.             Instantiate (shot, shotSpawn3.position, shotSpawn3.rotation);
    51.         }
    52.         if (shot4 == true) {
    53.             Instantiate (shot, shotSpawn4.position, shotSpawn4.rotation);
    54.         }
    55.         if (shot5 == true) {
    56.             Instantiate (shot, shotSpawn5.position, shotSpawn5.rotation);
    57. //        }
    58.  
    59.             GetComponent<AudioSource> ().Play ();
    60.         }
    61.  
    62.     }
    63.    
    64.     void OnTriggerEnter(Collider other)
    65.     {
    66.         if (other.gameObject.CompareTag ( "Pick Up"))
    67.         {
    68.             other.gameObject.SetActive (false);
    69.             count = count + 1;
    70.             SetCountText ();
    71.         }
    72.     }
    73.    
    74.     void SetCountText ()
    75.     {
    76.         countText.text = "Count: " + count.ToString ();
    77.         if (count >= 5) {
    78.             winText.text = "Level Up 1";
    79.             gameObject.SetActive (shot1 = false);
    80.             gameObject.SetActive (shot4 = false);
    81.             gameObject.SetActive (shot5 = false);
    82.             gameObject.SetActive (shot2 = true);
    83.             gameObject.SetActive (shot3 = true);
    84.            
    85.  
    86.         }
    87.         if (count >= 10) {
    88.             winText.text = "Level Up 2";
    89.             gameObject.SetActive (shot2 = false);
    90.             gameObject.SetActive (shot3 = false);
    91.             gameObject.SetActive (shot1 = true);
    92.             gameObject.SetActive (shot4 = true);
    93.             gameObject.SetActive (shot5 = true);
    94.        
    95.         }
    96.         if (count >= 15) {
    97.             winText.text = "Level Up 3";
    98.             gameObject.SetActive (shot4 = false);
    99.             gameObject.SetActive (shot5 = false);
    100.             gameObject.SetActive (shot1 = true);
    101.             gameObject.SetActive (shot2 = true);
    102.             gameObject.SetActive (shot3 = true);
    103.  
    104.  
    105.         }
    106.         if (count >= 20) {
    107.             winText.text = "Level Up 4";
    108.             gameObject.SetActive (shot1 = false);
    109.             gameObject.SetActive (shot2 = true);
    110.             gameObject.SetActive (shot3 = true);
    111.             gameObject.SetActive (shot4 = true);
    112.             gameObject.SetActive (shot5 = true);
    113.         }
    114.         if (count >= 25) {
    115.             winText.text = "Level Up 5";
    116.             gameObject.SetActive (shot1 = true);
    117.             gameObject.SetActive (shot2 = true);
    118.             gameObject.SetActive (shot3 = true);
    119.             gameObject.SetActive (shot4 = true);
    120.             gameObject.SetActive (shot5 = true);
    121.         }
    122.        
    123.     }
    124. }
     
  39. SolidWhiteTiger

    SolidWhiteTiger

    Joined:
    Oct 26, 2015
    Posts:
    4
    Hey, was wondering why the need to put the weapon_player audio clip in the player transform and then create code to call that clip in the same instance that it calls a bolt. Why not put the clip in the bolt prefab, that way when the script calls the bolt prefab, the clip will play on awake?
     
    Kaotis likes this.
  40. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you post your code and paste your errors? You can copy and paste the error from the lower box in the console when selecting the error.

    If your game WAS working and the new joystick code was causing problems, then you should only revert back to the last
    saved point where everything was working. You don't need to destroy all of your work back to the start of the lessons.

    If you have thrown everything out, and didn't have any incremental saves, you can start with the "Done" scene in the "Done" folder.
     
  41. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please post your code (using code tags please!) and any details from the inspector as screenshots?

    This I'm not sure what you've done - but can you see anything in your code that could be blocking your movement? Have you checked your scripts against the scripts on the lesson page?
     
  42. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    First, I'd have a quick thought about devices and resolutions:
    http://theantranch.com/blog/some-issues-with-optimizing-your-game-to-all-resolutions/

    And then ask us any specific questions that you might have.
     
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    So, yes - this lesson is designed for a fixed resolution.

    There are a few things that are set hard to the current screen resolution. These include the background, the boundary, and spawn positions.

    To make these completely device independent, these will need to be calculated on a device per device basis.

    We do this in the "Hat Trick" collection game in the live training section for calculating the spawn positions.

    Have a look at that code and see how far you get, and then ping us here if you need more help.
     
  44. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    In this particular case it would make little or no difference... BUT! If we think about it, the sound really comes from the muzzle of the gun. If the sound was associated with the bolt, the sound would travel with the bolt as it played. The "bang" or "pop" could come from the muzzle, and the "whizz" or "whoosh" could come from the missile, to be more exact. But in this case we keep all the sound associated with the muzzle. As it's on the muzzle, we play it using the player's controller script.
     
  45. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You can also leverage the "active" property on a GameObject to make this a bit easier.

    First, make a list or array for your shotSpawn:
    Code (csharp):
    1. public Transform[] shotSpawns;
    With this, you'll be able to drag all of your shotSpawn transforms into the list.

    Next, in the inspector, set all of your shotSpawns - except the first one - to be inactive.

    Then in void Fire() you can simply say:
    Code (csharp):
    1.     void Fire ()
    2.     {
    3.         foreach (var spawn in shotSpawns) {
    4.             Instantiate (shot, spawn.position, spawn.rotation);
    5.         }
    6.  
    7.        GetComponent<AudioSource> ().Play ();
    8.    }
    This way, you are simply instantiating a shot at every active spawn.

    Now, to get this to work, you'll need to activate your shots in some order...

    ... but ...

    ... this code completely confuses me:
    Code (csharp):
    1.  
    2.         if (count >= 5) {
    3.             winText.text = "Level Up 1";
    4.             gameObject.SetActive (shot1 = false);
    5.             gameObject.SetActive (shot4 = false);
    6.             gameObject.SetActive (shot5 = false);
    7.             gameObject.SetActive (shot2 = true);
    8.             gameObject.SetActive (shot3 = true);
    9.      
    10.  
    11.         }
    The code gameObject refers to the GameObject that this script is attached to.

    SO - this code is setting this GameObject 5 times based on the boolean value of shotX = false;

    I'm not sure this is what you are wanting to do, is it?

    I assume that you want to turn on and off each shotSpawn.

    Because we are saving the shotSpawns as a Transform type, you will need to take the shotSpawn.gameObject and then use SetActive(bool) on that.

    Why don't you give that a go, and see what you can do with that, and ping us if you have any trouble.
     
  46. aidenlgrayson

    aidenlgrayson

    Joined:
    Oct 23, 2015
    Posts:
    5
    Hello,
    I have definitely taken it from the prefabs in the assets.
     
  47. muhammadtahiriqbal

    muhammadtahiriqbal

    Joined:
    Jun 27, 2015
    Posts:
    8
    i download spaceshooter and run in unity5 thats giving erorr

    Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported in Unity 5.
    If you want to use a non-convex mesh either make the Rigidbody kinematic or remove the Rigidbody component. Scene hierarchy path "Done_Player", Mesh asset path "Assets/Models/vehicle_playerShip_collider.FBX" Mesh name "player_ship_collider"
     
  48. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    See above...
     
  49. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is described in the FAQ at the top of the thread, and in the annotations on the video, and in the upgrade guide.
     
  50. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    OK, What can you give us to help us diagnose this issue. The usual one, is that the instantiated shot uses (or reuses) a bolt that's been instantiated in the scene - so when the last one is destroyed, there is nothing referenced to instantiate...

    Can you redescribe your issue? And give us as much supporting evidence as possible?