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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Space Shooter Tutorial Q&A

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

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This doesn't look like the code from the project... you can check against the video, or the script included on the page where the video is embedded (or lastly, the "done" folder in the project!)

    In this particular case, you are not assigning the value to the rigidbody. You set "position" to be equal to the value of the Rigidbody's position (Where do you declare "position"? Are you getting any errors in the console??), then you set "position" again to be equal to the new Vector3 with the clamp code... Think of "position" as a box where you store numbers or a piece of paper where you write a value. You've set that value twice, the second time overwriting the value written the first time... then... do nothing with it. So this box, or piece of paper have some value attached to it, but it's never used. You need to assign this value back to the Rigidbody.velocity.

    If I remember correctly, the code is more like GetComponent <Rigidbody>.velocity = new Vector3 (Mathf.Clamp (...
     
  2. Minos32

    Minos32

    Joined:
    Jun 22, 2015
    Posts:
    24
    Hi!! I'm following the tutorial and I'm now in the asteroid and ship explotions part but I have a little problem: My ship "shoots" one time at the beginning of the game always, so, if i don't move the asteroid to another place I cannot probe the ship explotion because the bullet destroys the asteroid since the game starts. How ca I fix it?? I have unity 5 and I checked my code with the videos and scripts in the Done folder and my code is correct (as I can see).
     
    Last edited: Oct 15, 2015
  3. Minos32

    Minos32

    Joined:
    Jun 22, 2015
    Posts:
    24
    Forget it, I found how to fix it: deselecting the box aside the name of the object (Bolt) :p
     

    Attached Files:

    Last edited: Oct 15, 2015
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm not sure what stage of the project you are at, but: At one stage we temporarily use a bolt in the scene to visualize the placement of the ShotSpawn GameObject, but... afterwards we delete it! If you are beyond that point, don't just deactivate it, as you've done in your second post, delete it just like in the video!
     
    Minos32 likes this.
  5. Algoriddem

    Algoriddem

    Joined:
    Jul 3, 2015
    Posts:
    2
    Thank you :)
     
  6. dksingh20

    dksingh20

    Joined:
    Oct 16, 2015
    Posts:
    1
    Hi, I am having issue with GUI text, tried everything but it is not getting visible on the screen.
    Please help
     
  7. LionFische

    LionFische

    Joined:
    Oct 16, 2015
    Posts:
    107
    Unity 5.1.2

    Hi, any suggestions as to what I need to do here. I'm getting the following error.

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

    I'm guessing it's to do with my creation of a new rigid body object.

    Code:

    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. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public float tilt;
    14.     public Boundary boundary;
    15.    
    16.     public GameObject shot;
    17.     public Transform shotSpawn;
    18.     public float fireRate;
    19.    
    20.     private float nextFire;
    21.     private Rigidbody rb;
    22.    
    23.     void Start ()
    24.     {
    25.         rb = GetComponent<Rigidbody>();
    26.     }
    27.  
    28.     void Update ()
    29.     {
    30.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    31.         {
    32.             nextFire = Time.time + fireRate;
    33.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    34.         }
    35.     }
    36.    
    37. void FixedUpdate ()
    38.     {
    39.         float moveHorizontal = Input.GetAxis ("Horizontal");
    40.         float moveVertical = Input.GetAxis ("Vertical");
    41.        
    42.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    43.         rb.velocity = movement * speed;
    44.        
    45.         rb.position = new Vector3
    46.             (
    47.                 Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    48.                 0.0f,
    49.                 Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    50.                 );
    51.        
    52.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    53.     }
    54. }
     
  8. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Is this adding the initial Text to the screen when following the video?. When watching the video tutorial for Counting Points and Displaying the Score, ensure you have the annotations on, as there is extra information for getting the GUIText up and running.

    Also ensure that the Transform is adjusted to what is shown in video to X=0.5, Y=0.5, Z=0 this will show text in middle of screen as this I believe uses viewport Coordinates as opposed to world space.

    Give it the default Arial font, and beef up the font size to say 20, just so you can see it.

    see if that works.
     
    Last edited: Oct 16, 2015
    jaymacibe likes this.
  9. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836

    Is that the only error you see in your console window?

    You can always pop this into your Start() method, just to confirm that it is finding your Rigidbody, least then you can rule that out.

    Code (CSharp):
    1. void Start ()
    2.     {
    3.         rb = GetComponent<Rigidbody>();
    4.         // Just to confirm that it is being set in Start()
    5.         if(rb == null)
    6.         {
    7.             Debug.LogWarning("Rigidbody not set!");
    8.         }      
    9.         else
    10.         {
    11.             Debug.Log("Rigidbody set to : " + rb);
    12.         }
    13.  
    14.     }
     
    Last edited: Oct 16, 2015
  10. boylamn

    boylamn

    Joined:
    Jan 28, 2015
    Posts:
    19
    I have added a menu that's shown first when i start my game, then from it i can click on paly button to start playing the game and it's working like a charm when i test it in unity. But surprisingly when i installed the game on my device and trying to play the game by clicking on play button it takes a long time to start the game (it start first the background music and after some seconds, the game then begin) and I didn't expect this and I hope you can help me to solve this issue.

    NB: My game is built for Android platform.
     
    Last edited: Oct 17, 2015
  11. LionFische

    LionFische

    Joined:
    Oct 16, 2015
    Posts:
    107
    Hi, thanks for the reply. I also have the following message.

    'The referenced script on this Behaviour is missing!'

    All my scripts are in a Scripts Folder and have been working fine up until this point.


    LF.
     
  12. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    I would be looking at all the gameobjects in your scene first, then your prefabs.

    what you are looking for will be in the inspector, where there should be a script component, there will be a yellow triangle stating that the script is missing or invalid.

    if you find it, just readd the script to that object.

    This can come about, if you have renamed the class in one of the scripts, and not renamed the file name of the script (as these must be the same)
    or moving scripts around, sometimes it can lose the connection.
     
  13. LionFische

    LionFische

    Joined:
    Oct 16, 2015
    Posts:
    107
    Hi thanks, again for the reply. I removed the script component from my PlayerController and then re-added it. The errors have vanished. Thanks, was going google eyed looking at the code for errors!
     
    OboShape likes this.
  14. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @boylamn,
    what platform are you targetting?
    (i dont have much mobile experience, but I know its going to be asked ;))
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please, can you give us some more information? Being a bit cheeky, I could ask: is your monitor plugged in and turned on? Are you running unity? We have no possible way to answer this without more detail! (^_^)!
     
  16. boylamn

    boylamn

    Joined:
    Jan 28, 2015
    Posts:
    19
    Android
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There shouldn't be a lot to load here. What device are you testing on? You may want to ask on the android forums for more detailed help.
     
  18. boylamn

    boylamn

    Joined:
    Jan 28, 2015
    Posts:
    19
    I'm using an android tablet
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ack! Which one? In that, if I were using "an iPhone" and I had terrible performance, and it turns out I'm running the original iPhone, that may just be an issue with the hardware. If you have the latest top of the line Galaxy Note, then that's a different can of worms.

    TBH, I have virtually no experience with android. You may want to hit up the android section of the forum (under platform specific) and ask there.

    If you find anything surprising, let us know.
     
  20. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    There is a little bit of info here, relating to android profiling.
    http://answers.unity3d.com/questions/32368/how-do-i-profile-on-an-android-device.html

    are you putting the APK on the device, or are you using Unity Remote?

    if you have the APK in an online repo somewhere, i can try it on an old tablet i use for testing if you want.
    Would give me an idea what your seeing.

    PM me a link if you have it online anywhere.
     
  21. jaymacibe

    jaymacibe

    Joined:
    Oct 11, 2015
    Posts:
    3
    Hi, I'm on the step 11 (Game Controller) part of the tutorial.

    I got everything copied down for the script and added the script to the Game Controller object.
    Game Controller is tagged as such as well.
    When I hit play, nothing happens, no spawning asteroids.

    I even tested by putting a Debug.log step on the Start method. No message gets printed in the console.
    I have no idea why the script is not being called at all.

    Here is the script:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class gameController : MonoBehaviour {
    5.  
    6.     public GameObject hazard;
    7.     public Vector3 spawnValues;
    8.  
    9.     void start()
    10.     {
    11.         Debug.Log("hi");
    12.         SpawnWaves();
    13.      
    14.     }
    15.  
    16.     void SpawnWaves()
    17.     {
    18.         Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    19.         Quaternion spawnRotation = Quaternion.identity;
    20.  
    21.         Debug.Log("SpawnValues.X: " + spawnValues.x);
    22.         Debug.Log("Spawn: " + hazard);
    23.  
    24.         Instantiate(hazard, spawnPosition, spawnRotation);
    25.     }
    26. }
    I placed the Asteroid parent game object to the hazard variable.

    I tested the game while in play and manually added an Asteroid and it works; falling down, hitting player, explosions! But that is it, nothing gets spawned code wise.
     
  22. jaymacibe

    jaymacibe

    Joined:
    Oct 11, 2015
    Posts:
    3
    Found it. "start" should be "Start".

    There was no compiler errors.

    I guess the game thought I was making a different method instead of the prebuilt Start method.
     
  23. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yes. Capitalisation is important. Start and start are two different names. So is starT and sTart, so be careful with your typing!
     
    jaymacibe likes this.
  24. dermotphysics

    dermotphysics

    Joined:
    Oct 17, 2015
    Posts:
    2
    HI! I am having a problem starting with Unity 5.1, I have just begun the Spaceshooter tutorial, and I cannot get the Game view to change to create a top down perspective. no matter what, it always remains a horizontal perspective.

    Thanks in advance,

    upload_2015-10-17_16-30-12.png
     
  25. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Hi,

    if you can have another look at the video for the camera and lighting, ensuring that you follow the changes to the Main camera transform and rotation values.

    http://unity3d.com/learn/tutorials/projects/space-shooter/camera-and-lighting?playlist=17147


    Oh as a bit more info, see the Axis gizmo in the top right of your scene view, just under it you have something that looks like =ISO, if you click it it will change to <-PERSP. this toggles your scene view between isometric and perspective. just in case it looks a little funky as your working through the tutorial.
     
    Last edited: Oct 17, 2015
  26. dermotphysics

    dermotphysics

    Joined:
    Oct 17, 2015
    Posts:
    2
    Thank you so much!! It is correct now!
     
  27. cornel68

    cornel68

    Joined:
    Sep 9, 2015
    Posts:
    1
    This Script not function in Unity 5...Why?
    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;

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

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

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

    rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
    }
     
  28. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning,

    If you could, in future posts can you wrap code in Code Tags (see sig for help link) just makes it a whole lot easier to read code, especially when it gets a bit longer)

    with this code, you will receive an error that shows in the console, something like the following
    as it would have stated, this is an issue that Unity can resolve automatically for you, and would have popped up a message box asking you to upgrade the code.

    Remember the console is your friend, and wants to tell you loads of useful errors and warnings that it thinks you should know :)

    basically since we cannot access the Rigidbody component directly, we could use the GetComponent<Rigidbody>() every time, but it makes more sense to grab it once from within the Start method and use that each time throughout our script.

    Hope that makes sense.


    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. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public float tilt;
    14.     public Boundary boundary;
    15.     // need to declare this here so we can use it later
    16.     // just named it 'rb' so theres no confusion later
    17.     Rigidbody rb;
    18.  
    19.     // need to add the Start method so we can initialise the rigidBody variable we declared above
    20.     void Start()
    21.     {
    22.         // get and store the Rigidbody, as in Unity 5 direct access to Rigidbody has been depricated
    23.         // so we store it to use it later
    24.         rb = GetComponent<Rigidbody>();
    25.     }
    26.     void FixedUpdate ()
    27.     {
    28.         float moveHorizontal = Input.GetAxis ("Horizontal");
    29.         float moveVertical = Input.GetAxis ("Vertical");
    30.      
    31.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    32.         rb.velocity = movement * speed;
    33.      
    34.         rb.position = new Vector3
    35.             (
    36.                 Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax),
    37.                 0.0f,
    38.                 Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
    39.                 );
    40.      
    41.         rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    42.     }
    43. }
     
  29. Mieng

    Mieng

    Joined:
    Oct 18, 2015
    Posts:
    3
    Hi,

    When I'm building a standalone for Windows the score text ends up outside of the game boundaries in the black area (at the top left of the screen). Is there a good way to ensure that it always ends up in the same position regardless of build target?

    also, is there an easy code to add that shuts down the executable when pressing a key ('Escape' for example)?

    thanks for any help :)
     
  30. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning,

    For the Score text are you using the GuiText as in the video or the UI Canvas?

    as far as I can remember the GUIText using the viewport coordinates and the position should remain at that coords through scaling etc.
    but if the text is going off screen to the left, Check that the Anchor for the GUIText component is set to Upper Left. and add a bit of X and Y padding.

    for Quiting the standalone player, have a look here at this thread
    http://forum.unity3d.com/threads/how-to-quit-a-pc-or-mac-game-stand-alone-player.35646/
     
  31. Mieng

    Mieng

    Joined:
    Oct 18, 2015
    Posts:
    3
    I'm using the GuiText component as in the video. I've tried building the done scene called "Done_Main" also and i get the same problem. it seems like the alignment of the GuiText component is based on the screen and not the viewport. I tried messing around with the player settings for standalone builds but it didnt help. It looks and plays fine in the editor though, this behaviour is only when running the built executable...
     
  32. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Not sure, maybe Adam could offer more advice.

    But as far as I was aware screen space is measured in pixels and where the GUIText resides is catered by viewport space, ie (0,0) is lower left and (1, 1) is upper right.
    Whereby independent of scale and sizing the viewport is always 1 high and 1 wide.
    Not sure why yours would be going off screen tho.
     
    Last edited: Oct 18, 2015
  33. McNoguff

    McNoguff

    Joined:
    Sep 20, 2015
    Posts:
    9
    Thank you for posting the additional chapter with the content from the live session!!
     
  34. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I should have a complete and detailed upgrade guide for Unity 5 available in the next day or so as well.
     
    jaymacibe and McNoguff like this.
  35. Gkinder

    Gkinder

    Joined:
    Oct 21, 2015
    Posts:
    3
    Ran into a major stumbling block here. I've rerun the Space Shooter 05 a few times and walked through it very carefully. I've rewritten the PlayerController a few times. When I run the scene, the ship sort of flashes rapidly. The Z position in the inspectorr seems to jump between the 2 values for zMin and zMax. I can move left to right but up and down does nothing. I tried the rb = GetComponent<Rigidbody>() thing with the same result. (The same way it was used in the roll a ball tutorial, and as shown in a few code snippets in this thread.) Is there a setting outside of the PlayerController that I've messed up or missed?

    The code currently is:
    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. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public Boundary boundary;
    14.  
    15.     void FixedUpdate()
    16.     {
    17.         float moveHorizontal = Input.GetAxis ("Horizontal");
    18.         float moveVertical = Input.GetAxis ("Vertical");
    19.  
    20.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    21.         GetComponent<Rigidbody>().velocity = movement * speed;
    22.  
    23.         GetComponent<Rigidbody>().position = new Vector3
    24.             (
    25.                 Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    26.                 0.0f,
    27.                 Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    28.             );
    29.     }
    30. }
    31.  
     
  36. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What values do you have in the inspector?
     
  37. Gkinder

    Gkinder

    Joined:
    Oct 21, 2015
    Posts:
    3
    Thanks Adam, you asked the question and as I went to get you the answer I tried switching the zMin and Zmax values and it worked. Here is the way I had it and the ship was flashing between 8 and -4 on the z axis. I switched them and
    all is working. What I dont understand now, as shown in the second screen shot, is that the orientation of the ship says that my zMax should be positive, but it only works if its a negative. <confused> Is my orientation turned around?


    Capture.PNG

    Capture2.PNG
     
  38. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The second screenshot seems like it should be the correct one, and should work. Does it not?
     
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    SicraS and OboShape like this.
  40. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    @OboShape Give it a look-thru, when you get the chance (no pressure) and let me know if you see anything missing or mistaken.
     
  41. Gkinder

    Gkinder

    Joined:
    Oct 21, 2015
    Posts:
    3
    It certainly does work. My confusion was coming from My Camera/Game view and my Scene view. My Camera's viewable area was fom -4 to +14 on the z, but in scene view I was seeing the bottom of the background quad and that was all the way down at -14. So, I was just all screwed up. I have the new guide now and will be using that as well. Thanks again.
     
  42. tonyrh

    tonyrh

    Joined:
    Oct 23, 2015
    Posts:
    1
    Thanks for the Upgrade Guide!
    On page 21, section 01. AUDIO this:

    To access the local Rigidbody component, please use
    “GetComponent<AudioSource>().” It is best to “cache” this reference
    in a local variable when the instance of the script is initialized.
    Please declare a local member variable to hold the Rigidbody
    reference ...

    should be:

    To access the local AudioSource component, please use
    “GetComponent<AudioSource>().” It is best to “cache” this reference
    in a local variable when the instance of the script is initialized.
    Please declare a local member variable to hold the AudioSource
    reference ...

    [Highlight by Mod for clarity - my apologies]
     
    Last edited by a moderator: Oct 23, 2015
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Whoops - good point. Copy and paste-itis!

    As there is a bit of overhead for updating the document, rer-writing the .pdf, hosting the file and updating the links, I will wait for a bit to see if there is any more errata to correct.

    Thanks for the report!
     
  44. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166


    I expect to put sign on the door "Fighters only!". No man was born as experience coder. We all were noobs.
    It is constant fight, to learn, to improve yourself and overcome obstacles. If those shy creatures doesn't have "fighter" inside or you don't make them fighters as good teacher, you just attracted them like bee on a flower, they will purchase Unity dreaming of making games, so easily, like things were done by their parents for them.
    By selling them pirate clothes( wear in wrong way (crossref, tightcoupaling...) you didn't make them pirates. (a.k.a basic tutorial). Even as baby pirates, when the start breathing sea air they need to breath subclassing MonoBehaviour, reflection by "name" binded callbacks and strategy pattern, fast, so can start walking. (Which is not piece of cake). So give bit rum form the start.
    They also need constant training( not to repeat myself how many interm, adv, master tutorials are on the site???).
    On first sound of cannon shotting, they will abandon ship( abandon Unity).
    Even worst is if they succeed make the game and selling the game. Frustration come when cos of "mem leak" (what is that? noob made the game) crash on random level after 50th level and sold 400copies, or change update content after year(what this reference was for in the spaghetti)....or else....ship will sink. Redo de game from scratch or hire someone from 400copies to patched for you,...patch here patch there...=>redo de game. I've been hired like this, to fix, on many occasions. When I saw what was problem, patching was an option but I always succeeded to pursway the owner understand his error building on bad fondation and to rebuild.
     
  45. j.mcleod

    j.mcleod

    Joined:
    Oct 7, 2015
    Posts:
    3
    When trying to fire shots with the boundary a shot comes out and is immediately destroyed.

    DestroyByBoundary.cs
    using UnityEngine;
    using System.Collections;
    publicclassDestroyByBoundary : MonoBehaviour
    {
    void OnTriggerExit(Collider other)
    {
    Destroy(other.gameObject);
    }
    }

    PlayerController.cs
    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;
    public GameObject shot;
    public Transform shotspawn;
    public float fireRate;
    private float nextFire;
    private Rigidbody rb;
    void Start()

    {
    rb = GetComponent<Rigidbody>();

    }
    void Update ()
    {
    if (Input.GetButton("Fire1") && Time.time > nextFire)

    {
    nextFire = Time.time + fireRate;
    GameObject clone =
    Instantiate(shot, shotspawn.position, shotspawn.rotation) asGameObject;

    }
    }
    void FixedUpdate ()

    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    Vector3 movement = newVector3 (moveHorizontal, 0.0f, moveVertical);

    rb.velocity = movement * speed;
    rb.position = newVector3
    (
    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);

    }
    }
     
  46. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @j.mcleod,

    If you can bud, in the future please use code tags when posting code snippets. makes it a whole lot easier to read in a post ;)

    I take it you are working through the Shooting shots video?

    What I would do first is to have a think about what could be destroying the bolt and do a couple of checks to narrow things down a bit.

    firstly, if we only have the boundary and the player, then we could confirm that it is the boundary that is destroying the bolt or not.

    within your DestroyByBoundary script, add a little debug line in to check to see if the boundary is triggering the bolt to be destroyed in the OnTriggerExit function.
    Code (CSharp):
    1. void OnTriggerExit(Collider other)
    2.     {
    3.         // if you put this debug line in, this will show a message stating what the boundary destroyed.
    4.         Debug.Log ("Boundary Destroyed : " + other.name);
    5.         Destroy(other.gameObject);
    6.     }
    save and test. fire a shot and see what happens. if you see a console message stating that the boundary did indeed destroy your bolt then this narrows it down slightly.

    Based on the above being the culprit, we now know that the bolt is deviating from the normal forward motion and has some Y axis travel somewhere.
    On your Bolt, check use gravity is unchecked.
    On your player, ensure that your shotSpawn child object doesnt have any rotation at all.

    you disable boundary, press play, fire a shot and then pause after a second and check the heirarchy to see where abouts the Bolt is by highlighting it in the heirarchy and checking its transform in the inspector.

    Just a couple of things to have a look at to narrow down the gremlins location :)
     
  47. sosunny1103

    sosunny1103

    Joined:
    Oct 25, 2015
    Posts:
    1
    I am having a trouble with tilt function. So far, tilt function is my last function of the entire script (C#) and it is exactly same with the one provided in the video. But it is keep giving me the error saying, "The best overload method match for 'UnityEngine.Quaternion.Euler (float, float, float)' has some invalid arguments Argument '#3' cannot convert 'UnityEngine.Vector3' expression to type 'float'.

    Could you help finding the solution for this?
     
  48. j.mcleod

    j.mcleod

    Joined:
    Oct 7, 2015
    Posts:
    3
    The Boundary Destroyed the bolt. There is no rotation on the shot spawn. After disabling the boundary and firing the position of the bolt becomes after a few moments x= -1.6974, y= 2.23517, z= 74.4503
     
  49. j.mcleod

    j.mcleod

    Joined:
    Oct 7, 2015
    Posts:
    3
    BTW my shoots move super slow, speed 20 doesn't produce double the spacecraft speed and the bolt jumps to the next position, choppy.
     
  50. rckkk

    rckkk

    Joined:
    Oct 8, 2015
    Posts:
    6
    Hello, please help me with an issue that i'm having on the SpaceShooter tutorial when i'm creating the restart button i'm doing just the same as the video lessons, when you get the RestartButton and put the game controller on the button script area on the video you get some options on the side and you get the Done_GameController.RestartGame and at mine isn't show that just appear the option to put a Mono Script > string name and then when i start the game and crash my spaceship the restart button appear but doesn't work
     

    Attached Files: