Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Space Shooter Tutorial Q&A

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

  1. skrap1r0n

    skrap1r0n

    Joined:
    May 31, 2016
    Posts:
    3
    I hadn't set it up as a Prefab, I will go back and look at the video, but I believe the tumble action was applied before it was made into a prefab.

    I wend ahead and did it in my game, it still didn't tumble. Turns out, that is Kinematic was checked. as soon as I unchecked it, it tumbled.

    Edit: I went back and looked, it never said to make it kinematic, so I am not sure how that happened. I may have accidentally hit is when turning off Use Gravity. Making it a prefab is in video 2.3. At any rate, it works. Thanks.
     
    Last edited: Jun 3, 2016
  2. alexwebbb

    alexwebbb

    Joined:
    May 18, 2016
    Posts:
    1
    Heyoo,

    Great tutorial, just wanted to say that I exported it to WebGL with no problems.. maybe something to consider integrating into the official upgrade guide now that the web player is close to being fully deprecated..

    Not sure if it mattered, but I also utilized sceneManager instead of Application.LoadApp (or whatever it was).

    Thanks!
     
  3. emirrichan

    emirrichan

    Joined:
    Jun 4, 2016
    Posts:
    2
    Hi, Im doing the space shooter tutorial and something popped up in my coding (PlayerController) and Im not quite sure how to fix it. A System.NullReferenceException has occured.

    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class Boundary
    {
    public float xMin, xMax, zMin, zMax;
    }
    public class PlayerController : MonoBehaviour
    {
    private Rigidbody rb;

    public float speed;
    public float tilt;
    public Boundary boundary;

    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;

    private float nextFire;

    void Update ()
    {
    if (Input.GetButton ("Fire1") && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    // GameObject clone =
    Instantiate(shot, shotSpawn.position, shotSpawn.rotation); // as GameObject;
    }
    }

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rb.velocity = movement * speed;

    rb.position = new Vector3
    (
    Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    );
    rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    }
    }
     
  4. emirrichan

    emirrichan

    Joined:
    Jun 4, 2016
    Posts:
    2
    Hi there, I'm doing the space shooter tutorial and a problem that said NullReferenceException and im not quite sure how to fix it. HelP?
     

    Attached Files:

  5. SpaceUnicorn

    SpaceUnicorn

    Joined:
    Apr 15, 2016
    Posts:
    2
    Hi!

    I'm working on the tutorial, and I have an issue with the Explosion part. I have this code:
    "
    using UnityEngine;
    using System.Collections;

    publicclassDestroyByContact:MonoBehaviour
    {
    publicGameObjectexplosion;
    publicGameObjectplayerExplosion;

    voidOnTriggerEnter(Colliderother)
    {
    if(other.tag=="Boundary"){
    return;
    }
    Instantiate(explosion,transform.position,transform.rotation);
    if(other.tag=="Player"){
    Instantiate(playerExplosion,other.transform.position,other.transform.rotation);
    }
    Destroy(other.gameObject);
    Destroy(gameObject);
    }
    }
    "

    It seems correct, and I followed the steps in the video, but when the ship and the player collide nothing happens, there is no explosion at all. (the explosion works with the bolt). I'd like any kind of help to figure out what is wrong :/
     
  6. Jovy83

    Jovy83

    Joined:
    Feb 28, 2015
    Posts:
    15
    Just reposting my previous questions: I got a few questions regarding the "Extending Space Shooter with Enemies, Hazards, BG scorlling, etc" video.

    How did you know that the screen size is 12 and 40 at 1:30:56 of the video? How did you figure out the size?

    Also for the scrolling background, why did we initially position the duplicate background at the bottom of the original background? We needed to scroll it down for the game, so why not initially position it at the top? What is the difference?

    Also why did we need to set a boundary for the enemy ship on the Z axis? I mean I understand we need the boundary for the X axis so that it doesn't go offscreen on left/right. We don't need to bound the enemy on the Z because once it exceeds the bottom of the screen, its path is over and gets destroyed by our bounding box right?

    Thanks
     
  7. ChajusSaib

    ChajusSaib

    Joined:
    Jun 10, 2016
    Posts:
    1
    Hello lads!

    So I've followed every tutorial there is(19 or 17??) for the Space Shooter and have deployed to Mobile and web. Web with using HTML5(WebGL) has no problems but on mobile, the player can move out to the left or right of the screen and asteroids and enemy ships spawn and move out of the scene view as well.

    It's perfectly fine with web view and using the remote app to control when testing. Any suggestions. I've followed and the boundaries are -6 and 6.

    Thank you!
     
  8. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,

    Adam put together an upgrade guide to help with completing this tutorial with Unity 5, you an view it here
    https://oc.unity3d.com/index.php/s/rna2inqWBBysn6l
    (page 14 i think covers the moving the player updates)

    Looks like you may have accidentally missed out the Start method. currently 'rb' is Null, so if we use it here it will throw an error.
    but if you add the Start method, we can set rb to reference the Rigidbody, and it should be good to go.
    Code (CSharp):
    1. void Start ()
    2. {
    3.      rb = GetComponent<Rigidbody>();
    4. }
     
  9. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836

    Is it when the Asteroid and the Player collide there is no explosion from either the player or the asteroid?
    but you can shoot a Bolt and have it destroy the asteroid and show the asteroid explosion?

    Are you getting any errors being displayed in the console at all?

    Have a quick check, is the collider on the player to see if its the same as the video.
     
    Last edited: Jun 13, 2016
  10. mrhaunt

    mrhaunt

    Joined:
    Jun 14, 2016
    Posts:
    4

    Having the same issue, I've put in the code as described but it still throws an error for 'unexpected symbol rb'. Any suggestions?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     void Start ()
    7.     {
    8.         rb = GetComponent<Rigidbody> ();
    9.     }
    10.  
    11.     void FixedUpdate ()
    12.     {
    13.         float moveHorizontal = Input.GetAxis ("Horizontal");
    14.         float moveVertical = Input.GetAxis ("Vertical");
    15.  
    16.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical)
    17.             rb.velocity = movement
    18.     }
    19.  
    20.  
    21.  
    22. }
    23.  
     
  11. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    You didn't put the code as described. You forgot:
    Code (CSharp):
    1. Rigidbody rb;
    just before
    Code (CSharp):
    1. void Start ()
    ;)
     
  12. mrhaunt

    mrhaunt

    Joined:
    Jun 14, 2016
    Posts:
    4
    Should it be public or private, neither? Tried all three and it still gives the error.
     
  13. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    It's private. When nothing is mentioned, it's automatically private. If it was public, you would have to assign a value to it in the inspector. :)

    Did you put the line at the correct place?
    Code (CSharp):
    1. public class PlayerControllerScript : MonoBehaviour
    2. {
    3.     private Rigidbody rb;
    4.  
    5.     void Start ()
    6.     {
    7.         rb = GetComponent<Rigidbody>();
    8.     }
    Did you save your script before getting back to the editor?
     
  14. mrhaunt

    mrhaunt

    Joined:
    Jun 14, 2016
    Posts:
    4
    Ah, I see!

    Alright, I have that in like so:

    Code (CSharp):
    1. private Rigidbody rb;
    2.     void Start ()
    3.     {
    4.         rb = GetComponent<Rigidbody> ();
    5.     }
    However, when I later try to use rb.velocity, it still gives an error. :confused: Saved, and even in monodevelop it doesn't recognize it. I'm puzzled.

    Eta: Placement checks out too, confirmed.
     
    Last edited: Jun 14, 2016
  15. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    Please post your entire script.
     
  16. mrhaunt

    mrhaunt

    Joined:
    Jun 14, 2016
    Posts:
    4
    Here we go:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     private Rigidbody rb;
    7.  
    8.     void Start ()
    9.     {
    10.         rb = GetComponent<Rigidbody> ();
    11.     }
    12.  
    13.     void FixedUpdate ()
    14.     {
    15.         float moveHorizontal = Input.GetAxis ("Horizontal");
    16.         float moveVertical = Input.GetAxis ("Vertical");
    17.  
    18.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical)
    19.             rb.Velocity = movement
    20.     }
    21.          
    22.  
    23. }
    24.  
    Not sure what's correct but I've also tried velocity both capitalized and uncapitalized, if that helps.
     
  17. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    You must really pay attention when you write a script. You forgot the ";" at the end of the vector3 line and in the line "rb.velocity = movement", movement should be within parenthesis and followed by ";".
     
    OboShape likes this.
  18. nntb

    nntb

    Joined:
    Jun 18, 2016
    Posts:
    3
    ok there is the astroid destruction part of the tutorial thats all like its so easy just do this

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByTime : MonoBehaviour
    5. {
    6.     public float lifetime;
    7.  
    8.     void Start ()
    9.     {
    10.         Destory(gameObject, lifetime);
    11.     }
    12.  
    13.  
    14. }
    15.    
    only my compiler is all like 'Destory' is a 'type' but is used like a 'variable'

    i i dont get what i am doing wrong T.T
     
  19. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    Watch your spelling: it's Destroy not Destory.
     
  20. nntb

    nntb

    Joined:
    Jun 18, 2016
    Posts:
    3
    thanks for the fast reply. i caught it earlyer and came here to delete my post... your too fast XD Lol
     
  21. nntb

    nntb

    Joined:
    Jun 18, 2016
    Posts:
    3
    i made a mistake

    GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    gameController = gameControllerObject.GetComponent <GameController> ();

    was suposed to be

    GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    gameController = gameControllerObject.GetComponent<GameController>();

    but its fixed now
     
    Last edited: Jun 18, 2016
  22. AliHaidry

    AliHaidry

    Joined:
    Jan 19, 2016
    Posts:
    5
    Hello Adam!
    Really admire the way, you helped us to learn the gaming environment in Unity 3D.

    For now I would like to know that how to connect my joy-stick(Stratus XL Wireless Gaming Controller for Windows and Android) with my cell(Sony Xperia-m / S7-edge). I tried but couldn't come up with any solution!
     
  23. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    Adding or removing spaces is of no effect, at least in this case. :)
     
  24. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have you checked the manufacturers site for compatability and instructions?

    I think that would be a good place to start as ,as far as I understood, it would be up to the device and platform to provide generic input data for unity to read.

    <edit>
    Ok I've googled it, might find info here that may assist
    http://www.bluetoothdoctor.com/ques...e-will-not-connect-to--steelseries-stratus-xl
     
    Last edited: Jun 19, 2016
  25. AliHaidry

    AliHaidry

    Joined:
    Jan 19, 2016
    Posts:
    5
    How do I supposed to quit my application using GUI-Button! I have tried this piece of code "Application.Quit()" , but this won't work.
     
  26. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Are you testing in the editor or a build?
    Just asking, as far as I am aware it doesn't quit if testing in the editor.

    Any errors or warnings showing out of curiosity?
     
  27. AliHaidry

    AliHaidry

    Joined:
    Jan 19, 2016
    Posts:
    5
    Thanks! issue has been resolved.
     
  28. AliHaidry

    AliHaidry

    Joined:
    Jan 19, 2016
    Posts:
    5
    How do I supposed to set my controls for the 2nd scene(have made a second level for 'Space Shooter'),everything is working fine! Except player movement, fire area, ship movement and GUI-buttons (restart,exit).
     

    Attached Files:

  29. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    How did you create the second scene, did you just copy the first one and rename it (assuming it was functioning correctly)?

    Taking the first part, your player movement, whats not happening the way it should?
     
  30. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Code (CSharp):
    1. void OnTriggerEnter (Collider other)
    2.     {
    3.         if (other.tag == "Boundary" || other.tag == "Enemy")
    4.         {
    5.             return;
    6.         }
    7.  
    8.         if (explosion != null)
    9.         {
    10.             Instantiate(explosion, transform.position, transform.rotation);
    11.         }
    12.  
    13.         if (other.tag == "Player")
    14.         {
    15.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    16.             gameController.GameOver();
    17.         }
    18.        
    19.         gameController.AddScore(scoreValue);
    20.         Destroy (other.gameObject);
    21.         Destroy (gameObject);
    22.     }
    DestroyByContact script is the best optimal script. I cannot code like this. One script is attached to three object which are Astroid, Enemy and EnemyBolt. And, there is some logic in "OnTriggerEnter" function.
    My question is "How Can I write this kind of efficient code". My programming skill is not good.
    I want to write optimal and efficient code.
     
  31. paefantasy

    paefantasy

    Joined:
    Jun 19, 2016
    Posts:
    1
    can anyone help me ? because i've been trying to code the asteroid to move randomly as

    at 9.11
    but it doesn't work so what should i do ?
     
  32. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    I' still a n00b myself, and still learning, but I remember people telling me to " Practice a little and practice often", the language is one thing, but how you use it is more a craft, and that it will come together over time with regular practice.
     
    KAYUMIY likes this.
  33. AliHaidry

    AliHaidry

    Joined:
    Jan 19, 2016
    Posts:
    5
    Well I made a new scene! Saved it as "LevelTwo" afterwards I copy all the accessories of scene one to my scene two (LevelTwo) that's what I did for my new scene! but all the controls and everything is just working fine for my scene one! Its just the Player Controllers that I'm troubling with for my second scene(LevelTwo), rest is working fine as well for second scene(LevelTwo).
     
  34. technano

    technano

    Joined:
    Jan 13, 2016
    Posts:
    23
    So I'm trying to use the segment of code used to make the boundary for the player so that they don't go out of the play area, in my own little game and I get an error which confuses me because nothing is really different between the two.
    Here's the error:
    Assets/Scripts/Movement.cs(20,17): error CS1922: A field or property `UnityEngine.Vector3' cannot be initialized with a collection object initializer because type `UnityEngine.Vector3' does not implement `System.Collections.IEnumerable' interface

    I don't understand what the error means at all and would like some clarification on it.
    This is also what my code looks like!
    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. public class Movement : MonoBehaviour {
    10.  
    11.     public float speed;
    12.     public Boundary boundary;
    13.  
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.         transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime,0f,speed * Input.GetAxis("Vertical") * Time.deltaTime);
    18.  
    19.         GetComponent<Rigidbody> ().position = new Vector3
    20.         {
    21.             Mathf.Clamp (GetComponent<Rigidbody> ().position, xMin, xMax),
    22.             0f,
    23.             Mathf.Clamp (GetComponent<Rigidbody> ().position, zMin, zMax)
    24.         };
    25.     }
    26. }
    27.  
    Thank you so much for the help!
     
  35. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning, see if I can try and help you out in the meantime.

    Theres nothing wrong with the way you have set the Boundary class up and instantiated it, as it appears the same as the tutorial.
    The issues you are facing are a few fold within your Update() method, although this is not as within the tutorial, I'll try and point you in the right direction if I can..

    Looking at lines 20 and 24, you currently have these { } braces when you are creating a new Vector3, these should be ( and ).

    If you look at how you are using Mathf.Clamp() within your new Vector3() statement, theres a couple of interesting things to be aware of here.

    creating a new Vector3() takes in three float values, so we must ensure that each one is a float.

    so when we are using Mathf.Clamp in this case we must ensure that what we are telling it to clamp to is a float, and the minimum and maximums are also float types.

    looking at this line;
    Code (CSharp):
    1. Mathf.Clamp (GetComponent<Rigidbody> ().position, xMin, xMax)
    for the first part, you are getting the Rigidbody position here, which is a Vector 3 (x, y and z components), what we really need is just the 'x' part of it, so adding '.x' to the end will give you the float value for the first part.

    secondly, xMin. if you look at the top xMin is a variable held within your Boundary class. you created an instance of your Boundary class called 'boundary' so to get the public xMin value from that class we would use the (dot) . accessor.
    boundary.xMin would give you that float value.

    So what I ended up with was the following, hope it helps you out a bit (sorry for the rambling a bit, been away for a while and getting back into the swing of things)

    Code (CSharp):
    1. void Update () {
    2.         transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime,0f,speed * Input.GetAxis("Vertical") * Time.deltaTime);
    3.  
    4.         GetComponent<Rigidbody> ().position = new Vector3
    5.             (
    6.                 Mathf.Clamp (GetComponent<Rigidbody> ().position.x, boundary.xMin, boundary.xMax),
    7.             0f,
    8.                 Mathf.Clamp (GetComponent<Rigidbody> ().position.z, boundary.zMin, boundary.zMax)
    9.             );
    10.     }
     
  36. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Hi,

    Looking at that screenshots, what you appear to be getting is errors related to things that have been updated in Unity since the original tutorial was released.

    I'm not sure if your watching via YouTube or on the Learn site here.
    http://unity3d.com/learn/tutorials/projects/space-shooter-tutorial

    on the Learn site page for this tutorial (link above), you will see a link do download the Unity5 upgrade guide and it will show you in there what minor changes are required.
    Also, when watching the videos on the page, if you switch annotations on it should highlight some issues also.
     
  37. technano

    technano

    Joined:
    Jan 13, 2016
    Posts:
    23
    Thank you so so much!! you explained it so clearly for me, it was very helpful as well. So just checking to see hat I understand it correctly. Adding the ".x" to the Rigidbodies position gives the Mathf.Clamp just the X position of the rigidbody? Also Fixing my code got rid of that error and I can enter playmode, but when I try and move the player around I can only move with the left and right buttons and when I do move it's in a diagonal line. I'm not to sure why that would be happening? And thank you again for the help!
     
  38. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    when you are using this..
    Code (CSharp):
    1. GetComponent<Rigidbody> ().position
    this is giving you a Vector3 type, as in a position in 3 dimensional space in this case, now a Vector3 can be broken down into (X, Y , Z) components. so if we wanted just the X float value component of that vector3, we could use, for example
    myVector3.x, or if we wanted the z value component we could use, myVector3.z.

    ensure that, in lines 6 and 8 in the code I pasted above, that you are indeed using the x value on line 6, and z value on line 8.
    you will tend to see diagonal movement if you are only using one value for both (ie both x, or both z)

    Have you already completed this tutorial, and going back to try some different things out?
    If you are working through this tutorial for the first time, may I suggest working through it with the same code and implementation that Adam uses and explains and follow along with the video and upgrade guide on the learn page this will ensure that everyone is the same and easier to assist.
     
    Last edited: Jun 28, 2016
  39. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Code (CSharp):
    1. [System.Serializable]
    2. public class Done_Boundary
    3. {
    4.     public float xMin, xMax, zMin, zMax;
    5. }
    boundary.png
    Why do we need this code? Why do we need to create another Class for setting boundary.
    We can do it without Boundary Class. Right? Whats advantage does Boundary Class have? This technique is Editor Scripting code (in my opinion). But, I don't understand role of Editor Scripting here.
     
  40. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    At this point with the tutorial its for neatness as it gives us a nice turndown in the inspector, and since its in a class of its own it can be reused if needed.

    as far as the Serialization goes, I'ts all still beyond me, as far as I understand Serialization is at the heart of alot of things in Unity. as far as this Boundary class goes, since it is not derived from a MonoBehaviour we can tell Unity to serialize it to expose the properties within the inspector as otherwise it wont go near it and we see nothing.
    I think, using Editor scripting is like rewriting the inspector component, but in this case, we just want to tell Unity that we want to serialise/deserialise this data to allow us to see it.

    Im sure Adam will explain when he gets back, and give you better info.
     
    Last edited: Jun 28, 2016
    KAYUMIY likes this.
  41. technano

    technano

    Joined:
    Jan 13, 2016
    Posts:
    23
    Thank you so much again! You're a great help, I understand it all much better now! And yes I've already completed the tutorial and I'm just using what is explained here with my own project. Sorry I'm posting here instead of asking on the other forums. I just wasn't getting much help there so I came here.
     
    OboShape likes this.
  42. Rosealinda

    Rosealinda

    Joined:
    Jun 28, 2016
    Posts:
    1
    I'm doing the space shooters game and I'm trying to spawn an Enemy (one of those other ships that fire back) everything was going alright until the evasive maneuvers script. When I added that the ship doesn't move but spawns randomly in front of me. I was wondering what was wrong.
     

    Attached Files:

  43. JigsawStudios

    JigsawStudios

    Joined:
    Jun 30, 2016
    Posts:
    1
    Please help I have run into a problem whilst doing the extended tutorial. the enemy bolt is not destroying the player... the destroy on contact script looks identical to the 'done' version. what could be causing this fault?

    thank you
     
  44. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    Make sure the colliders are set "is trigger" for both the player and the bolt.
     
    KAYUMIY likes this.
  45. balzua

    balzua

    Joined:
    Jun 28, 2016
    Posts:
    3
    Hi, I'm having a strange error where the game runs fine in Play Mode in the Unity Editor but when I build the game either for Web Player or Mac OS X, the asteroids / player are not destroyed.

    Explosions and sound effects still play but the game objects are not removed as they should be.
    Also, I am not seeing my score text on screen (could be an unrelated issue). I am not sure about Game Over text since I'm not able to trigger it because I can't actually get the ship to be destroyed.

    Again all the issues I'm having are in the actual build of the game, everything runs correctly in the Unity play mode.

    Edit: Nevermind, I went back and discovered that I actually had been building with older scene that got saved as a different name by mistake.
     
    Last edited: Jun 30, 2016
  46. Urenemas

    Urenemas

    Joined:
    Jul 1, 2016
    Posts:
    2
    Hi, I am having a problem with the "Creating Shots" part of this tutorial. My "Mover" script is not working; the bolt does not move at all when placed in the hierarchy.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mover : MonoBehaviour
    5. {
    6.     private Rigidbody rb;
    7.  
    8.     public float speed;
    9.  
    10.     void start ()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.         rb.velocity = transform.forward * speed;
    14.     }
    15. }
    16.  
    Edit: Ok, I found that I did not capitalize the S in "Start" but now when I move the bolt into the hierarchy it appears then vanishes without any sense of movement.

    Edit 2: The vanishing is happening because the bolt is moving on the wrong axis and going behind the background image. How am I supposed to set this straight?

    Edit 3: Ok I must have oriented the scene wrong because it works as intended when I use up instead of forward. Thank fully monodevelop gives me info when I mouse over code or else I would be stuck.
     
    Last edited: Jul 1, 2016
  47. VladElena

    VladElena

    Joined:
    Jul 11, 2016
    Posts:
    1
    Hello! Doing lesson 9 faced with a problem. The asteroid not only rotates, but also moves. How can I fix it?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomRotator : MonoBehaviour
    5. {
    6.     public float tumble;
    7.  
    8.     void Start()
    9.     {
    10.         GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * tumble;
    11.     }
    12. }
     
  48. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    I think they are supposed to move anyway, so, just carry on to the next chapter. ;)
     
  49. BobDDstryr

    BobDDstryr

    Joined:
    Jul 12, 2016
    Posts:
    1
    I'm running into a few problems, and have a few questions. My Code Sample will follow my message.

    I've been trying to follow the tutorial - and have noticed the annotations about differences between Unity 4 and Unity 5.

    I'm currently at the step for scripting player movement - but I was running into a problem where when the ship hit the boundary, it was wasn't stopping. It *was* slowing down a lot - but it would keep going.

    I did some searching online, and found someone recommending adding the "Update()" function, with calls to set the transform position - because the call in fixedUpdate only changes the rigidbody position.

    So - I tried that, and now the ship *does* stop when it runs into the boundary - however, the boundary is now *tacky*; after hitting the boundary, I need the hold the opposite direction down for a bit to get the ship to move away. More specifically - if I hit the left boundary, and continue holding left for 5 seconds, I then need to hold right for at least 5 seconds for the ship to begin moving to the right again.

    I currently have Unity version 5.3.5f Personal (64-bit) installed on my system.

    My questions are as follows:

    1 - Is adding Update() to change the transform position the right thing to do? If so, why would I need both update and fixedUpdate? If not, what I should I do instead? I'd really like not just to get this working, but to understand why this is happening.

    2 - My suspicion is that the reason the ship is sticking to the sides, is because some value is continuing to increase/decrease when I'm holding that direction. The ship is then staying at the correct position due to the Boundary functions - but when I press the other direction I need to wait for this value to get back to its proper range for the ship to begin moving again. I tried adding Mathf.Clamp commands around the Input.GetAxis commands, but realized that they have a value between -1 and 1 - and so I had no idea what they'd need to be clamped at. And looking around several pages of the Q&A nobody else seems to have had to do something like that...

    At any rate, any help would be greatly appreciated. Thanks.



    Code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. [System.Serializable]
    6. public class Boundary
    7. {
    8.     public float xMin, xMax, zMin, zMax;
    9. }
    10.  
    11. public class PlayerController : MonoBehaviour {
    12.  
    13.     public float speed;
    14.     private Rigidbody rb;
    15.  
    16.     public float tilt;
    17.  
    18.     public Boundary boundary;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         transform.position = new Vector3
    28.         (
    29.                 Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
    30.                 0.0f,
    31.                 Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
    32.         );
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void FixedUpdate () {
    37.         float movehorizontal = Input.GetAxis("Horizontal");
    38.         float movevertical = Input.GetAxis("Vertical");
    39.  
    40.         Vector3 movement = new Vector3(movehorizontal, 0.0f, movevertical);
    41.  
    42.         rb.AddForce(movement * speed);
    43.  
    44.         rb.position = new Vector3
    45.             (
    46.                 Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
    47.                 0,
    48.                 Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
    49.             );
    50.  
    51.         rb.rotation = Quaternion.Euler(0, 0, rb.velocity.x * -tilt);
    52.  
    53.     }
    54.  
    55. }
     
    Last edited: Jul 12, 2016
  50. jigsy1

    jigsy1

    Joined:
    Nov 21, 2015
    Posts:
    7
    Hi, I am having some trouble with my player controller script. I am receiving an error message: "NullReferenceException: Object reference not set to an instance of an object PlayerController.FixedUpdate ()" on line 32.
    I have been over the lesson an still cant find a cause for the error, there is no red in my monodevelop script, can someone give me a hand?
    Thanks

    using UnityEngine;
    using System.Collections;

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



    public class PlayerController : MonoBehaviour {

    public float speed;
    public float tilt;
    public Boundary boundary;
    private Rigidbody rb;


    void start()
    {
    rb = GetComponent<Rigidbody>();
    }

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

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rb.velocity = movement * speed;

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

    rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);


    }


    }