Search Unity

Space Shooter Tutorial Q&A

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

  1. panoskal

    panoskal

    Joined:
    Mar 31, 2017
    Posts:
    28
    dougpowell - I may be taking a shot in the dark, but have you removed the mesh collider from the VFX quad that's inside the Bolt prefab?
     
  2. dougpowell

    dougpowell

    Joined:
    Mar 16, 2017
    Posts:
    6
    Thanks all. I got it working by turning is Kinematic Off. Now the Bolt fires and there is no message in the console.
     
  3. dragonpoe

    dragonpoe

    Joined:
    Mar 30, 2017
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [System.Serializable]
    4. public class Boundary
    5. {
    6.     public float xMin, xMax, zMin, zMax;
    7. }
    8. public class PlayerController : MonoBehaviour
    9. {
    10.     private Rigidbody rb;
    11.     void start ()
    12.     {
    13.         rb = GetComponent<Rigidbody> ();
    14.     }
    15.     public float speed;
    16.     public float tilt;
    17.     public Boundary boundary;
    18.     void FixedUpdate ()
    19.     {
    20.         rb = GetComponent<Rigidbody> ();
    21.         float moveHorizontal = Input.GetAxis ("Horizontal");
    22.         float moveVertical = Input.GetAxis ("Vertical");
    23.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    24.         rb.velocity = movement * speed;
    25.         rb.position = new Vector3
    26.             (
    27.                 Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    28.                 0.0f,
    29.                 Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    30.             );
    31.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    32.     }
    33. }
    I'm using this code. But ship still won't move.

    I get this error:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:31)

    Please help.

    Thank you.
     
  4. panoskal

    panoskal

    Joined:
    Mar 31, 2017
    Posts:
    28
    dragonpoe - At line 11 start needs to be capitalized, Start.

    Also, why do you get the Rigidbody component inside the FixedUpdate at line 20. I compared it to mine and I don't have it, not that I know enough to absolutely correct you on that one.
     
  5. dragonpoe

    dragonpoe

    Joined:
    Mar 30, 2017
    Posts:
    4
    Corrected the line 11 start to Start. Still the ship won't move.
     
  6. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    As panoskal said, you don't need the second Rigidbody getcomponent in line 20. make sure to take that out. With the correction to Start, everything else looks good (going from memory). Make sure you have entered a speed value in the inspector and make sure that isKinnematic is not selected on the Rigidbody component. And just to be safe, make sure the script is attached to the player ship.
     
  7. dragonpoe

    dragonpoe

    Joined:
    Mar 30, 2017
    Posts:
    4
    start to Start -> checked
    Rigidbody in line 20 -> Removed
    Speed = 10
    Tilt = 2
    isKinnematic = Off
    Script is attached.

    Result: The ship doesn't move a bit.
     
  8. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Running out of easy solutions :)

    Did you enter values for your Boundary class variables (xMin, xMax, zMin, zMax.) If they are at default 0 the ship won't move. You can also check your input manager, though that is rarely an issue. Edit>Project Settings> Input.

    When I have an issue I try to get back to a base working level. Maybe comment out line 25 - 31 and just use the basic move command and see if that works. then add the other stuff until it stops working and you have your issue.
     
  9. dragonpoe

    dragonpoe

    Joined:
    Mar 30, 2017
    Posts:
    4
    Thank you, guys! The issue resolved. I'm out of my ignorance haven't set the Boundary stats. Now when I did, it worked!

    Thanks again!
     
  10. MadScientist82

    MadScientist82

    Joined:
    Apr 7, 2017
    Posts:
    1
    Hi guys. I am new in both, Unity and this forum. The quantity and quality of the material you have is awesome, congrats! I followed this amazing tutorial and then I have implemented many changes (more enemies, power ups, explosions, evasion movements...). The thing is that when my player take a power up that gives him 2, 3 or 5 shots, if the player shot at the same time that tilt, some shots are disappearing, and I don't know why. I am almost sure that they are not destroying each other, since the player bolt doesn't have any script for destroying. Any idea whats happening?
    Thank you so much in advance.

    Edit: I have localized the line in the code which provokes this problem. It's in the PlayerController script (last line regarding "Quaternion.Euler"). When I comment this code line the ship doesn't tilt but all the shots work fine. I want both, that the ship tilts and fire various shots. Any idea about how can i fix this problem?

    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         Vector2 direction = touchPad.GetDirection ();
    4.         Vector3 movement = new Vector3 (direction.x, 0.0f, direction.y);
    5.         GetComponent<Rigidbody>().velocity = movement * speed;
    6.      
    7.         GetComponent<Rigidbody>().position = new Vector3
    8.         (
    9.             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    10.             0.0f,
    11.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    12.         );
    13.      
    14. //        GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    15.     }
     
    Last edited: Apr 7, 2017
  11. FlyingFishF

    FlyingFishF

    Joined:
    Apr 7, 2017
    Posts:
    1
    Hello,I was learning Moving the Player , and I have a question.
    We make our Player ship's position clamped between -6 to 6 in the fix update,
    But I found if I keep pressing right key the position value will reach 6.2 !
    Why caused this?
    2.png
     
  12. Mcdoylie

    Mcdoylie

    Joined:
    Apr 4, 2017
    Posts:
    4
    *** Lol nevermind, found my problem, was missing a bracket ***
    Hi, I'm new to unity and am having a problem with the spae shooter tutorial. I am at the end the game part of the tutorial and am getting a unexpected symbol fault but cannot figure out the problem. Any ideas? Thank you in advance.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour
    6. {
    7.     public GameObject hazard;
    8.     public Vector3 spawnValues;
    9.     public int hazardCount;
    10.     public float spawnWait;
    11.     public float startWait;
    12.     public float waveWait;
    13.  
    14.     public GUIText scoreText;
    15.     public GUIText gameOverText;
    16.     public GUIText restartText;
    17.    
    18.     private bool gameOver;
    19.     private bool restart;
    20.     private int score;
    21.  
    22.     void Start ()
    23.         {
    24.             gameOver = false;
    25.             restart = false;
    26.             restartText.text = "";
    27.             gameOverText.text = "";
    28.             score = 0;
    29.             UpdateScore ();
    30.             StartCoroutine (SpawnWaves ());
    31.         }
    32.  
    33.     void Update ()
    34.         {
    35.             if (restart)
    36.                 {
    37.                     if (Input.GetKeyDown (KeyCode.R))
    38.                     {
    39.                         Application.LoadLevel (Application.loadedLevel);
    40.                     }
    41.                 }
    42.  
    43.         IEnumerator SpawnWaves ()
    44.         {
    45.             yield return new WaitForSeconds (startWait);
    46.             while (true)
    47.             {
    48.                 for (int i = 0; i < hazardCount; i ++)
    49.                 {
    50.                     Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    51.                     Quaternion spawnRotation = Quaternion.identity;
    52.                     Instantiate (hazard, spawnPosition, spawnRotation);
    53.                     yield return new WaitForSeconds (spawnWait);
    54.                 }
    55.                 yield return new WaitForSeconds (waveWait);
    56.  
    57.                 if (gameOver)
    58.                 {
    59.                     restartText.text = "Press 'R' for Restart";
    60.                     restart = true;
    61.                     break;
    62.                 }
    63.             }
    64.         }
    65.     public void AddScore (int newScoreValue)
    66.     {
    67.         score += newScoreValue;
    68.         UpdateScore ();
    69.     }
    70.  
    71.         void UpdateScore ()
    72.         {
    73.             scoreText.text = "Score: " + score;
    74.         }
    75.     public void GameOver ()
    76.     {
    77.         gameOverText.text = "Game Over!";
    78.         gameOver = true;
    79.     }
    80. }
    81.  
     
    Last edited: Apr 8, 2017
  13. jayjay6115

    jayjay6115

    Joined:
    Aug 21, 2015
    Posts:
    1
    I started to try and do the wave i followed the code exactly but as soon as i did that my games stopped spawning enemies. Also can someone help me figure out how to upload c sharp scripts onto threads.

    [Added script - moderator]
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class laser : MonoBehaviour {
    7.  
    8.     public float speed;
    9.  
    10.     public GameObject Explosion;
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.         Vector3 pos = transform.position;
    15.  
    16.         Vector3 velocity = new Vector3 (0, speed * Time.deltaTime, 0);
    17.  
    18.         pos += transform.rotation * velocity;
    19.  
    20.         transform.position = pos;
    21.  
    22.         if (transform.position.y >= 5.8) {
    23.             delete ();
    24.         }
    25.  
    26.  
    27.     }
    28.  
    29.     void delete(){
    30.         Destroy (gameObject);
    31.     }
    32.  
    33.     void OnTriggerEnter2D(){
    34.         Destroy (gameObject);
    35.  
    36.         Instantiate(Explosion, transform.position, transform.rotation);
    37.  
    38.     }
    39.  
    40. }
     

    Attached Files:

    • laser.cs
      File size:
      632 bytes
      Views:
      843
    Last edited by a moderator: Apr 19, 2017
  14. gtodd876

    gtodd876

    Joined:
    Apr 10, 2017
    Posts:
    1
    I've searched this thread to find the solution to the now obsolete Application.LoadLevel which somebody said was:

    Code (CSharp):
    1. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    but adding that code gives me the error message:
    "The name `SceneManager' does not exist in the current context"

    Here is my GameController script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour {
    6.  
    7.     public GameObject hazard;
    8.     public Vector3 spawnValues;
    9.     public int hazardCount;
    10.     public float spawnWait;
    11.     public float startWait;
    12.     public float waveWait;
    13.  
    14.     public GUIText scoreText;
    15.     public GUIText restartText;
    16.     public GUIText gameOverText;
    17.  
    18.     private bool gameOver;
    19.     private bool restart;
    20.     private int score;
    21.  
    22.     void Start ()
    23.     {
    24.         gameOver = false;
    25.         restart = false;
    26.         restartText.text = "";
    27.         gameOverText.text = "";
    28.         score = 0;
    29.         StartCoroutine (SpawnWaves ());
    30.     }
    31.  
    32.     void Update ()
    33.     {
    34.         if (restart)
    35.         {
    36.             if (Input.GetKeyDown (KeyCode.R))
    37.             {
    38.                 //SceneManager.LoadScene("Main");
    39.                 //SceneManager.LoadScene(SceneManager.GetActiveScene());
    40.                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    41.             }
    42.         }
    43.     }
    44.  
    45.     IEnumerator SpawnWaves ()
    46.     {
    47.         yield return new WaitForSeconds (startWait);
    48.         while(true)
    49.         {
    50.             for (int i = 0; i < hazardCount; i++)
    51.             {
    52.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    53.                 Quaternion spawnRotation = Quaternion.identity;
    54.                 Instantiate (hazard, spawnPosition, spawnRotation);
    55.                 yield return new WaitForSeconds (spawnWait);
    56.             }
    57.         yield return new WaitForSeconds (waveWait);
    58.  
    59.             if (gameOver)
    60.             {
    61.                 restartText.text = "Press 'R' to Restart";
    62.                 restart = true;
    63.                 break;
    64.             }
    65.  
    66.         }
    67.     }
    68.  
    69.     public void AddScore (int newScoreValue)
    70.     {
    71.         score += newScoreValue;
    72.         UpdateScore ();
    73.     }
    74.  
    75.     void UpdateScore ()
    76.     {
    77.         scoreText.text = "Score: " + score;
    78.     }
    79.     public void GameOver ()
    80.     {
    81.        
    82.         gameOverText.text = "Game Over";
    83.         gameOver = true;
    84.     }
    85.  
    86. }
    87.  
    Any help getting past this hurdle would be greatly appreciated! I just want to re-load my scene when you press the 'R' key to restart.

    -Todd
     
  15. ICE_DragonM

    ICE_DragonM

    Joined:
    Feb 14, 2017
    Posts:
    7
    Hii guys,

    Its my first time I post here, I using unity 5.6 and I have a problem in Space Shooter tutorial, in the video "Add audio effects for the weapons, explosions and a background music track" I need to add the "explosion_asteroid" audio to the "explosion_asteroid" Prefab by drag and drop but I can't add audio clip into Prefab by drag and drop, it's dos not add it, any Suggestions?
     
    jimmack and andreyshade like this.
  16. ghhhr

    ghhhr

    Joined:
    Apr 11, 2017
    Posts:
    7
    Hi every one. I couldn't find it anywhere so I write it here. In setting up the game you advice to build game in Web Player. But there is no Web Player anymore. Do I think correct that I should use WebGL? Also a question: we are making these game in 3D or 2D? I've seen on movie that you left 3D mode in creating new project box, but the game window looks like the one for 2D games.
     
  17. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    ghhhr - Yes, use the WebGL. Web Player is not an option anymore. You will need to download the module, which it will do when you select WebGL.
    The game is made in 3D, but by using an orthographic camera it appears 2D.

    gtodd876 - you need to add the namespace that includes scene manager. So at the top of your script, where all the using statements are at, add using UnityEngine.SceneManagement;
     
    ghhhr and gtodd876 like this.
  18. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    JayJay6115,

    Adding scripts to the post is easy. In your scripting program highlight the code and use CTRL C to copy it. In your forum post select insert from the menu bar (just to the left of the floppy disc symbol), then select insert code. Use CTRL V to copy and done.
     
  19. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    ICE_Dragon_M, Did you add an Audio Listener to your prefab? If you have an Audio Listener, you should be able to drag the sound clip to the Audio Listener in the inspector.
     
  20. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10

    Sorry it took me a while to respond i just got out of spring break and back into school, so i took a break until i got settles in again. I've kept trying to change it and made sure the names of the bolts are different, along with each of the vehicles the bolts are assigned to are correct. It would be a great help if you opened your tutorial to figure out what you did, because this is the only thing stopping me from building this game
     
  21. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    MrSpaceChicken,

    My layout is like this. I have prefabs folder, in that I have a bolt and an enemy bolt. Each bolt has it's own vfx. When I highlight (select) one of the VFX components and it is in the inspector, then I drag either the orange bolt or the cyan bolt over to the inspector and it should change to that color. You need to drag it to the area below the button that says Add Component. Don't try to drag it onto the renderer part of the inspector. See if that helps.
     
  22. jay2104

    jay2104

    Joined:
    Apr 11, 2017
    Posts:
    1

    Hi,
    I tried doing exactly what the video says but i dont know why i am just not able to destroy those bolts.i am using unity 5.6.0f
     
  23. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    jay2104,

    Could you post your destroy by contact script (I believe that is the one) and the community can look it over for errors. Use code tags if you know how.
     
  24. Mackan9

    Mackan9

    Joined:
    Apr 12, 2017
    Posts:
    3
    Hello,
    I reinstalled Unity to the 5.6 and then I do the turtorial one more time. This time a notice a new problem. I guess this happends because the new version.

    In the lesson that we create a shot we rotate 90 degree and code following:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    public class Mover : MonoBehaviour {

    public float speed;
    private Rigidbody rb;

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

    }

    The problem is now that the bolt moves in two axis (Y and Z). If a remove the rotation the bolt moves in one direction. If I use transform.right or transform.up. This happend also.

    If I remove the code. The bolt don´t move.

    Why does the bolt move in two directions now?
     
  25. panoskal

    panoskal

    Joined:
    Mar 31, 2017
    Posts:
    28
    Mackan9 - I tried recreating your issue and I think what you did was rotate the Bolt Object by 90 degrees. What you need to do is rotate the child Object VFX by 90 degrees and have the parent Bolt at 0 rotation.
     
  26. Mackan9

    Mackan9

    Joined:
    Apr 12, 2017
    Posts:
    3
    panoskal.
    Thanks! You are right! Now it´s working.

    When I did the exercise a second time, I fastwatch the movies. I thought that in parentbolt the rotation will be and then put it into different shots. But they should not be. Parenbolt shall have no rotation at all.
     
  27. Mackan9

    Mackan9

    Joined:
    Apr 12, 2017
    Posts:
    3
    I wonder what to put on the parent level vs. child level. We create a parent object, and then a child object. You can adjust the position of both the parent object and child objects. Why do we make som changes to the parent object, and some changes of the children object? When making the enemy ship we adjust the position on the parent level and rotation on the child level.

    When we did astroiderna, we put collider at parent level. Then we hade to adjust the colliders of each parental level when we did the two extra astroiderna. Then I do not understand why you have collider at parent level if you still have to adjust so much. It would be faster to make an astroid and then copy it and change a little bit.

    I want to learn.
     
  28. iamfake

    iamfake

    Joined:
    Apr 14, 2017
    Posts:
    4
    I am currently on '7. Shooting Shots'. Problem is, When I am pressing the ctrl-key, Bolt clone is created but I am not able to see my ship shooting shots. When I drag manually my bolt prefab while in gameplay mode, my ship shoots.
    And Bolt clone is written in black, Is it supposed to be written in blue 'cuz it is a prefab?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. [System.Serializable]
    7. public class Boundry
    8. {
    9.  
    10.     public float xMin, xMax, zMin, zMax;
    11. }
    12. public class PlayerController : MonoBehaviour {
    13.     public Rigidbody rb;
    14.     public int speed;
    15.     public Boundry boundry;
    16.     public float tilt;
    17.     public GameObject shot;
    18.     public Transform shotspawn;
    19.     public float fireRate;
    20.  
    21.     private float nextFire;
    22.  
    23.     void start()
    24.     {
    25.         rb = GetComponent<Rigidbody>();
    26.  
    27.     }
    28.     void Update()
    29.     {
    30.      if(Input.GetButton("Fire1") && Time.time>nextFire)
    31.         {   nextFire = Time.time + fireRate;
    32.             Instantiate(shot,shotspawn.position,shotspawn.rotation);
    33.         }
    34.     }
    35.     void FixedUpdate () {
    36.         float movehorizontal = Input.GetAxis("Horizontal");
    37.         float movevertical = Input.GetAxis("Vertical");
    38.  
    39.         Vector3 movement = new Vector3(movehorizontal,0.0f,movevertical);
    40.         rb.velocity=movement*speed;
    41.         rb.position = new Vector3(
    42.             Mathf.Clamp(rb.position.x, boundry.xMin, boundry.xMax),
    43.             0.0f,
    44.             Mathf.Clamp(rb.position.z, boundry.zMin, boundry.zMax)
    45.             );
    46.         rb.rotation = Quaternion.Euler(
    47.             0.0f,
    48.             0.0f,
    49.             rb.velocity.x * -tilt);
    50.     }
    51. }
    52.  
     
  29. MrSpaceChicken

    MrSpaceChicken

    Joined:
    Mar 25, 2017
    Posts:
    10
    Thanks a lot! It was the vfxs that caused the problem it seems. I had to remake the full enemy bolt by copying off of the normal bolt, but it finally works with no bugs. I can finally build and run it!
     
  30. iamfake

    iamfake

    Joined:
    Apr 14, 2017
    Posts:
    4
    Hi I am not able to view my shots while in game play mode

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

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    In the video the clone is written in black also, so I think you are okay there. Did you make sure to drag your reference over to the inspector? What is the Ctrl key being used for? I think space bar is fire. Is Ctrl an alternate for Fire1?
     
  32. iamfake

    iamfake

    Joined:
    Apr 14, 2017
    Posts:
    4
    I think the problem is that the shots are being Instantiated outside the camera view
     
  33. AndiNguyen262

    AndiNguyen262

    Joined:
    Apr 17, 2017
    Posts:
    2
    Hi Adam-Buckner, i have an issue in Lesson8: Boundary. I can't enable the "DestroyByBoundary" Script.That my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyByBoundary : MonoBehaviour
    5. {
    6.     void OnTriggerExit(Collider other)
    7.     {
    8.         Destroy(other.gameObject);
    9.     }
    10. }
    I am using Unity 5.6.0f3. Hope your answer early :)
     

    Attached Files:

  34. a_Moon

    a_Moon

    Joined:
    Apr 17, 2017
    Posts:
    2
    Hello, I have a problem: ship continuing slowly moving and rotating at borders and flying off screen
     
  35. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    a_Moon,

    Could you post your player controller script. Not sure what might be causing that, but looking at the controlling script is a good start.
     
  36. ghhhr

    ghhhr

    Joined:
    Apr 11, 2017
    Posts:
    7
    Hi,

    I've finished this tutorial :) Well, almost. In the building game video I have some problem. First of all when I click build I do not have possibility to choose a name. The dialogue box asks me only to choose a folder. Maybe that's because I'm using windows 10? That's small problem because I called my project "space shooter". I've managed to deal with this by changing the name of project to "space_shooter".

    Much more important problem is after building game. In the movie you show that you have two files, one with all data and second is html type. I think it's because the project was made in web player. Now we use WebGL and the output is different. I have in my Builds folder two folders and one file outside of them. Only this file is of html type. So I opened it to play my game local. Unfortunately, I see logo of Unity, not working button to extend screen and... nothing more.

    I thought in the beggining that it's because my wrong name "space shooter" but changing space to underscore didn't help. May it be the problem with microsoft edge that is my default browser? Should I change it to firefox or anything else? Everything to this point was working correctly. I had some smaller challenges but in the end I solved them. But for this one I don't have an idea what to do.
     
  37. mrdmi

    mrdmi

    Joined:
    Jul 15, 2015
    Posts:
    2
    Hello all,

    I'm following the tutorial, just created the PlayerController script. When I hit play and move about, I get these odd black trails. I saved the scene, loaded the "Complete" scene and I'm not seeing this behavior at all. Any clues where to look? Has anyone seen this? Attached a screenshot.

    Thanks!

    upload_2017-4-17_23-3-48.png
     
  38. a_Moon

    a_Moon

    Joined:
    Apr 17, 2017
    Posts:
    2
    Solved. I had mistake in code
     
    Last edited: Apr 18, 2017
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    No, just the older ones. It would take a few months of production time to replace this video and rather than just take it down as "obsolete" due to changes in the editor, we keep it available and update it via pdf and YouTube annotations.

    You should get most of the changes via annotation, so turn them on.

    We do regularly add new learning material, but currently these are live training sessions.

    We do have several initiatives in the works for more and different lessons, but they all take time and resources and currently we are a team of 2-3 people.
     
  40. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    When creating scripts that do one thing and one thing only, they are easier to reuse and much more clear to write and design.

    Like the components we add to a GameObject (eg: Colliders, Rigidbodies, Cameras) we could make one script that did all of these things in one place, but then every GameObject would need one of these huge scripts with bits of it turned off, or you'd need to write a new huge script for every GameObject.

    This style of architecture is "componentized" and keeps each script or "component" discreet. Each component does one thing. If you need two behaviours on one GameObject, use two components.

     
  41. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What is calling "SpawnWaves"? Looking at this code, nothing is calling it.

    Now you could put this in an "Update" function, but it will be called every frame and then you'd have to, as you've done here, test the time and reset it.

    But - in your code, it would be very difficult to generate the waves.

    A while loop is a common way of looping though code.

    Part of the reason we chose that solution is to teach while loops and coroutines.

    By using a coroutine, we can elegantly pause the game actions as we need to. We have the wait at the top of the game so we can give the player a moment to get ready and we can then spawn each hazard with a small wait in between and lastly, in the same loop, create waves.

    To me, it's fairly compact and rather elegant.

     
  42. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is due to the fact that the ship is moving and then being set back, so if you hold the key down, every frame it will momentarily be just beyond the boundary. For this game, it's somewhat irrelevant, so it's not worth over complicating the session to address this.
     
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This doesn't look like the correct code for the waves and definitely is NOT from the tutorial...

     
  44. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I don't think you need to enable it. It should just work. There is no code other than OnTriggerExit, and that cannot be enabled or disabled.

     
  45. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What platform are you building to? In the video, we used the "Web Player" platform, and this is no longer available.

     
  46. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Check your camera. You may have changed the clear flags to something unexpected.

    This should be set to skybox or color.

     
    mrdmi likes this.
  47. khaledallen

    khaledallen

    Joined:
    Apr 19, 2017
    Posts:
    1
    I'm having trouble in the initial setup...there isn't a "web player" option for build settings, and the closest thing is WebGL. But that doesn't have many of the settings (resolution, eg) referenced in the tutorial.
     
  48. ghhhr

    ghhhr

    Joined:
    Apr 11, 2017
    Posts:
    7
    As I was adviced here I use WebGL.
     
  49. mrdmi

    mrdmi

    Joined:
    Jul 15, 2015
    Posts:
    2
    Absolutely right, my camera Clear Flags option was set to "Don't clear", no idea how. Thank you!
     
  50. ZeRuc

    ZeRuc

    Joined:
    Mar 3, 2017
    Posts:
    25
    It's called from Update, as you expected (I posted only relevant bits and not the entire script). Here's the entire code:

    Code (CSharp):
    1. //using System.Collections;
    2. //using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameController : MonoBehaviour {
    7.  
    8.     public GameObject[] hazards; //asteroids
    9.     public GameObject enemy; //enemy ship
    10.     public Vector3 spawnValues;
    11.     public GUIText scoreGUIText;
    12.     public GUIText restartGUIText;
    13.     public float spawnRate; //interval at which hazards will spawn
    14.     public int hazardCount;
    15.     int score = 0;
    16.     int spawnedCount = 0;
    17.     float waitTime = 0f;
    18.     string scoreText = "";
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         /*if (PlayerPrefs.GetString("language") == "en")
    23.         {
    24.             scoreText = "Score: ";
    25.             restartGUIText.text = "Press 'R' to restart";
    26.         }
    27.         if (PlayerPrefs.GetString("language") == "hr")
    28.         {
    29.             scoreText = "Bodovi: ";
    30.             restartGUIText.text = "Stisnite 'R' za restart";
    31.         }*/
    32.         LoadText(PlayerPrefs.GetString("language"));
    33.         UpdateScore();
    34.     }
    35.    
    36.     // Update is called once per frame
    37.     void Update () {
    38.         if (spawnedCount < hazardCount)
    39.             SpawnWaves();
    40.         else
    41.             //put the logic of spawning the next wave here
    42.         if (Input.GetKeyDown(KeyCode.R))
    43.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    44.         if (Input.GetKeyDown(KeyCode.Escape))
    45.             Time.timeScale = Time.timeScale == 0 ? 1 : 0;
    46.     }
    47.  
    48.     void SpawnWaves()
    49.     {
    50.         if (Time.time > waitTime)
    51.         {
    52.             GameObject hazard;
    53.             if (spawnedCount % 5 == 0)
    54.             {
    55.                 hazard = enemy;
    56.             }
    57.             else
    58.             {
    59.                 hazard = hazards[Random.Range(0, hazards.Length)];
    60.             }
    61.             Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    62.             Instantiate(hazard, spawnPosition, Quaternion.identity);
    63.             waitTime = Time.time + spawnRate;
    64.             spawnedCount++;
    65.  
    66.         }
    67.  
    68.     }
    69.  
    70.     void LoadText(string language)
    71.     {
    72.         switch (language)
    73.         {
    74.             case "hr":
    75.                 scoreText = "Bodovi: ";
    76.                 restartGUIText.text = "Stisnite 'R' za restart";
    77.                 break;
    78.             case "en":
    79.             default:
    80.                 scoreText = "Score: ";
    81.                 restartGUIText.text = "Press 'R' to restart";
    82.                 break;
    83.         }
    84.     }
    85.  
    86.     public void AddScore(int scoreValue)
    87.     {
    88.         score += scoreValue;
    89.         UpdateScore();
    90.     }
    91.  
    92.     void UpdateScore()
    93.     {
    94.         scoreGUIText.text = scoreText + score;
    95.     }
    96. }
    97.  
    But since you had the courtesy of replying to my question, I will bother you with another one:
    Why is Boundary (in "Moving the Player" part of the tutorial) a class and not a struct?