Search Unity

Space Shooter Tutorial Q&A

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

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The audio playing when the game starts and then not during the game sounds like "play on awake" is checked accidentally. Follow the instructions in the lesson for setting up the audio components.

    For this:
    ... Triple check the steps you are taking. Adding a component to a gameobject in the project window should not instantiate it in the scene. Could you be dragging the component in a way not shown in the video?
     
  2. AbsenceOfVoid

    AbsenceOfVoid

    Joined:
    May 1, 2017
    Posts:
    12
    Hi, newbie here! Could someone help me with this..? Using the code below does not seems to move my ship at all. If I replace the last line with

    Code (CSharp):
    1. rb.position = movement * speed;
    it moves but obviously not the way intended. What is the problem? Anyone? I'm using Unity 5.5.3f1. And I do realize this is not the final version but just puzzled that I don't get any movement with this intermediate version of script..

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

    AbsenceOfVoid

    Joined:
    May 1, 2017
    Posts:
    12
    Replying to myself... I tried unchecking the "Is Kinematic" option of my Rigidbody which I had activated at some point - and you can guess what happened.. It works..

    I'll leave my stupid talk to myself here in case someone else faces the same "issue".
     
    Last edited: May 3, 2017
  4. hewhohuntscats

    hewhohuntscats

    Joined:
    May 3, 2017
    Posts:
    7
    Hey folks. Got through RollyBall fine; and got through most of the Space Shooter even with the rigidbody syntax switches; but I'm stuck on the Destroy when Leave Boundary step.

    Basically, the boundary is set, the code is exactly from from the lesson, code examples, and the 'done' examples, but it doesn't destroy anything! It doesn't destroy bolts, and un-boxing the player ship's clamps so it can leave, it doesn't destroy that either.

    Furthermore I put in some debug window text, to set the status on the Boundary script as a text UI element, and it never even loads the initial 'loaded' debug text.

    Shots fire fine; kicking off from the shotspawn location.

    Everything compiles fine, no errors reported.

    Not quite sure what I'm doing wrong, so here's some code:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class BoundryScript : MonoBehaviour {
    6. public Text debugtext;
    7. private int count;
    8. void start ()
    9. {
    10. debugtext.text = "Boudnaryscript loaded";
    11. count = 0;
    12. }
    13. void onTriggerExit(Collider other)
    14. {
    15. Destroy(other.gameObject);
    16. debugtext.text = count.ToString ();
    17. count = count + 1;
    18. }
    19. }

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class BoltMover : MonoBehaviour {
    5. public Rigidbody rb;
    6. public float speed;
    7. void Start () {
    8. rb = GetComponent<Rigidbody>();
    9. rb.velocity = transform.forward * speed;
    10. }
    11. }

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

    upload_2017-5-3_18-16-9.png upload_2017-5-3_18-16-9.png
     
  5. hewhohuntscats

    hewhohuntscats

    Joined:
    May 3, 2017
    Posts:
    7
    Try setting the Speed variable in the inspector; public variables that don't have an initial value (like your 'speed') are 0, but you can change it in the inspector. IIRC the guide recommends 10.

     
  6. hewhohuntscats

    hewhohuntscats

    Joined:
    May 3, 2017
    Posts:
    7
    Update to this, made a wall with OnTriggerEnter with a new, seperate opject, and that also is not working.

    I'm not sure, still learning this, but I've been playing around with every way I can to detect the occurence or lack of a collision, with both collisions and triggers, and I think the game is just not detecting any collisions at all.


    Same exact behaviors and will also not remove the ship, confirmed 'Trigger' is check.
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class NorthWallDestroy : MonoBehaviour {
    5. void onTriggerEnter(Collider other)
    6. {
    7. Destroy(other.gameObject);
    8. }
    }


     
    Last edited: May 4, 2017
  7. gloomylumi

    gloomylumi

    Joined:
    May 2, 2017
    Posts:
    2
    Mickee27,

    I had the same problem. I got around it by manually adding an Audio Source component to the Prefabs, then dragging the audio clips into the AudioClip field.
     
  8. gloomylumi

    gloomylumi

    Joined:
    May 2, 2017
    Posts:
    2
    I'm getting this warning on the 'Ending the game' part of the tutorial:
    Assets/Scripts/GameController.cs(37,40): warning CS0618: `UnityEngine.Application.loadedLevel' is obsolete: `Use SceneManager to determine what scenes have been loaded'
    What is the not obsolete equivalent?
     
  9. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    I think it may be:
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     
  10. hewhohuntscats

    hewhohuntscats

    Joined:
    May 3, 2017
    Posts:
    7
    Lots of repeat questions, a beginning of thread 'hey this quesiton gets asked a lot and it's been answered at page #' would be neat.
     
  11. Deleted User

    Deleted User

    Guest

    Capital O with the OnTriggerEnter() from your code.

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7.  
    8. public class NorthWallDestroy : MonoBehaviour {
    9.  void OnTriggerEnter(Collider other)
    10.  {
    11.  Destroy(other.gameObject);
    12.  }
    13. }
    14.  
    Same on the code you're using on the posts above, capital O with the OnTriggerExit()
     
    Adam-Buckner likes this.
  12. Deleted User

    Deleted User

    Guest

    Replace the "LoadLevel" line with:
    Code (csharp):
    1. UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
    You can find docs here: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html
     
  13. hewhohuntscats

    hewhohuntscats

    Joined:
    May 3, 2017
    Posts:
    7
    I've got the o lowercase in the code; copy/pasting might have raised the case, but it's not a capitlization error (which usually returns an error)

    1. void onTriggerExit(Collider other)
    2. {


     
  14. Deleted User

    Deleted User

    Guest

    You need to have OnTriggerExit(), not onTriggerExit()

    Here there's no error with the lowercase or uppercase you could have a script called "onTriggerExit()" and call it, it would be totally valid.

    But Unity call the function "OnTriggerExit()" so even with your function correct, it is never called.

    You can even try to add to your code.
    Code (csharp):
    1.  Void OnTriggerExit(){ onTriggerExit();}
    It will totally work.
     
  15. hewhohuntscats

    hewhohuntscats

    Joined:
    May 3, 2017
    Posts:
    7
    Ugh, I feel like a noob. Thanks for catching that. Internet points for you, good sir.

     
  16. hewhohuntscats

    hewhohuntscats

    Joined:
    May 3, 2017
    Posts:
    7
    I apologize if I have dumb quesitons; but I'm having trouble grokking:

    Vector3 spawnPosition = new Vector3 ();
    Quaternion spawnRotation = new Quaternion ();

    This is [type 'vector/quat'] [name 'Spawn...'] [operator '='], adn that part makes sense.

    I'm not quite understanding what the 'new' does in these lines, though.
    Is it making a new object and defaulting it to default initialization settings for that object type? It it calling in null but making it a callable object? Something else?

    I can't find this in the scripting library just yet; (searching for 'new' gets a gazillion results) if someone could break this down what 'new' does or just provide me the documentation page so I can figure it out myself, that'd be awesome. :)
     
  17. Deleted User

    Deleted User

    Guest

    That's a construct you'll find in every OOP code.

    i'll try to explain it.

    Code (csharp):
    1.  
    2.  public class Exemple
    3. {
    4.     private int id;
    5.  
    6.  
    7. }
    In that exemple, you've got a class named "Exemple" who got only one parameter "id".

    When you are doing
    Code (csharp):
    1.  Exemple test;
    You are creating an empty Exemple object. There's nothing inside. if you are trying to get the id parameters, the id parameter is not only null, it doesn't exist at all.

    Code (csharp):
    1.  test = new Exemple();
    You are calling the construct function who will create parameters and initialize them to their default values. ( in that case, "null")

    In the Vector3 class the exact same thing is happening. By doing

    Code (csharp):
    1.  Vector3 myVector = new Vector3()
    You are calling the construct who will create the parameters of the Vector3 Object, and initialize them all to their default value.

    You'll have more information about this on google by seaching "C# class construct" (Can't help you with a link only docs i found on google was in french ^^' )

    edit: Got one from the US microsoft website. https://msdn.microsoft.com/en-us/library/ace5hbzh.aspx
     
    Last edited by a moderator: May 4, 2017
  18. Archivist0

    Archivist0

    Joined:
    May 1, 2017
    Posts:
    2

    The same issue happened with both the WebGL and a PC build. When adding the "Unity" logo stripe at the bottom it adds approximately 100 pixels to the bottom, and takes the equivalent amount of pixels at the top of your game area and moves them off the display area. Messing with the player settings in the build for output screen size did nothing but move the problem. Only work around I could find was to assume the top 100 pixels of you game was not visible and adjust accordingly. Thus a -100 in the GUIText. I am assuming with a paid version disabling the Logo would work. But this also means that if you wanted a bottom logo you would have to make it in the game and not in the build or deal with the screen loss.

    Thanks.
     
  19. jahendrie

    jahendrie

    Joined:
    May 3, 2017
    Posts:
    6
    Same issue here, with the ribbon on the bottom cutting off the top part of the screen. Not a huge thing I guess but this is something that should maybe be mentioned in the upgrade guide. Sticking these elements on the bottom instead of the top gets the job done but it's not really a solution as such.
     
  20. CorSec

    CorSec

    Joined:
    May 6, 2017
    Posts:
    2
    Ok I don't know what to do here.
    It thinks the open brackets on line 5 are linked to the close brackets on line 42.
    I pasted the code here as well as uploaded the actual file. If anyone could let me know what the issue is I would love to know.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour
    6. { //It opens here
    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 int score;
    16.  
    17.     private void Start()
    18.     {
    19.         score = 0;
    20.         UpdateScore();
    21.         StartCoroutine(Spawnwaves());
    22.     }
    23.     IEnumerator Spawnwaves()
    24.     {
    25.         while (true)
    26.         {
    27.             yield return new WaitForSeconds(startWait);
    28.             for (int i = 0; i < hazardCount; i++)
    29.             {
    30.                 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    31.                 Quaternion spawnRotation = Quaternion.identity;
    32.                 Instantiate(hazard, spawnPosition, spawnRotation);
    33.                 yield return new WaitForSeconds(spawnWait);
    34.             }
    35.             yield return new WaitForSeconds(waveWait);
    36.         }
    37.     }
    38.  
    39.     public void AddScore(int newScoreValue);
    40.     {
    41.         score += newScoreValue;
    42.         UpdateScore ();
    43.     } // and for some reason is closing here
    44.  
    45.     void UpdateScore ()
    46.     {
    47.         scoreText.text = "Score: " + score;
    48.     }
    49. }
    [code tags added by moderator]
     

    Attached Files:

    Last edited by a moderator: May 6, 2017
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please use code tags when posting code on the forums:

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

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please use code tags when posting code on the forums:

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

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are your colliders set as "Is Trigger" or are they traditional colliders.
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    See above: Colliders need to be set as "trigger colliders" or they won't be use "OnTriggerEnter" but "OnCollisionEnter".
    https://unity3d.com/learn/tutorials/topics/physics/colliders
    https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664

    If you can reproduce this behaviour please let me know. If you can't figure out what you were doing to make this happen, can you send in a project with this in a reproducible state, that would be great. I assume that this is some misunderstanding, as I've never heard of this before.
     
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Good catch.
     
  27. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please don't use "spoilers" to hide code. It's meant to be displayed so we can see it. If you have code tags, we can read it more easily and the code tag behaviour will keep it compact.
     
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This should be in the upgrade guide.
     
  29. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please report this as a bug using the in-editor bug reporter on a project with the completed space shooter project, make sure you submit the project, and get me the bug number?
     
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please report this as a bug using the in-editor bug reporter on a project with the completed space shooter project, make sure you submit the project, and get me the bug number?
     
  31. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This line:
    ... has an unnecessary semicolon.
     
  32. jahendrie

    jahendrie

    Joined:
    May 3, 2017
    Posts:
    6
    So it looks like I was a bit mistaken. tl;dr of it is that my browser window was too short.

    The game window, including the Unity banner, is ~950px tall (approx 50px of which is the banner). The banner doesn't cover up or replace elements of the gameplay area that I can tell. My display resolution at the moment is 1080p, so that combined with a non-maximized browser window and various UI chrome means the top of my browser was obscuring a little bit of the game area.

    I did initially try using the 'full size' option in the web player, but doing so places the text elements on the very far left and right sides of the screen, outside of the star field where I just didn't think to look for them at first. Maximizing the browser window doesn't do this, though, and renders the play area as expected.

    I can still submit a bug report if you'd like but it turns out to be operator error. Sorry about that.
     
  33. CorSec

    CorSec

    Joined:
    May 6, 2017
    Posts:
    2
    Thank you so much. =)

    And sorry about the code tags. I'll remember that in the future.
     
  34. ghhhr

    ghhhr

    Joined:
    Apr 11, 2017
    Posts:
    7

    Thank you for the answer. I discovered that I had "Run in background" checked. I started this project once again from the beginning with unchecked "Run in background". I've just finished and well, I don't see any difference. Everything works on my computer, but I cannot open it in a browser. Maybe a hint would be that if I try to open one of the files called "UnityLoader" (JavaScript file) I get a message stating that there is an error in syntax. Does it help anything?

    Please tell what to do with this. I would really like to create browser games which means that this tutorial is pretty important for me.

    Best regards.

    PS. I would also like to say that I've just build my project as PC standalone and everything works great. So there has to be some issue with this WebGL.
     
    Last edited: May 7, 2017
  35. artificialsurya

    artificialsurya

    Joined:
    May 7, 2017
    Posts:
    2
    I am a noobie in Unity. When I click on the camera, it does not show up anything in the inspector and it is stuck at Player. I am using 5.6. Any ideas will be greatly appreciated.
     
  36. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    possibly the inspector could be locked, and thereby not show any other objects properties when changing selection in heirarchy.
    have a look in the top right and see if the little padlock icon is locked (closed) or unlocked (open)
    upload_2017-5-7_21-16-58.png
     
  37. artificialsurya

    artificialsurya

    Joined:
    May 7, 2017
    Posts:
    2
    Thank you so much for the help. Really appreciate it!
     
    OboShape likes this.
  38. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you using a local file?

    If so, Chroma (and I believe Explorer) won't run the file due to security reasons.

    Use FireFox or host on the web?

    https://forum.unity3d.com/threads/unity-webgl-not-supported-on-google-chrome.306512/
     
  39. ghhhr

    ghhhr

    Joined:
    Apr 11, 2017
    Posts:
    7
  40. ghhhr

    ghhhr

    Joined:
    Apr 11, 2017
    Posts:
    7
    Hi,

    Thanks for all the help you give me so far. I have one question that maybe doesn't fit exactly here but I cannot find the answer elsewhere.

    In a tutorial 14 - "Counting points and displaying score" you discuss the issue of assigning instance of game controller to a prefab as a variable in script. It cannot be just D&D into a proper slot. You gave the way how to solve it using tag and function FindWithTag. Now I'm creating my own game and I have the same problem. But this time I want to assign an instance of text field in an input field to a prefab. And I see that there is no function FindWithTag... Can you say how may I solve it?

    I uploaded my script. It works as long as I assign instance of my input field to every instance of my game object. But it's not the way I want it to work :/
     

    Attached Files:

  41. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Oddly, the other solution is to "Build and Run" from Unity and Chrome will take the file. Oddly, you won't be able to open the file from the HDD, but Unity can serve it up with "Build and Run".
     
  42. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There is a live session on how to communicate between GameObjects and Components.

    A prefab is an asset that is independent of a scene. A scene may or may not exist and the prefab may or may not ever be used in a scene. Because of this, it doesn't make sense that a prefab can contain a reference to a scene object. It not only doesn't make sense, it's impossible.

    Let's say a homing missile prefab asset in our project has a Transform variable called target that's meant for the player, and the missile will chase this transform and try to destroy the player. Your game has 10 levels and each level has an instance of the player ship... How can the prefab asset in the project have a reference to ... any or all of them in their respective scenes?

    So, when the missile is fired (eg: instantiated into the scene), you need to assign the target transform to the missile. One way of doing this would be to tag the player with a "Player" tag and then do a FindGameObjectWithTag and use the results to set the target on the homing missile. Another solution would be for whatever script is calling the missile to be instantiated (say the GameController, or what-not) that this script has a reference to the local player's transform, and when the script instantiates the missile, the script retains a reference to the missile and pushes a reference to the missile for its target.
    Code (csharp):
    1. // pseudo code
    2. public Transform playerTransform;
    3. public GameObject myMissile;
    4. public Transform missileSpawnPoint;
    5.  
    6. void FireMissile()
    7. {
    8.    GameObject missile = Instantiate (myMissile, missileSpawnPoint.position, missileSpawnPoint.rotation) as GameObject;
    9.    missile.target = playerTransform;
    10. }
    As the GameController has a local instance, we can drag the Player onto the playerTransform slot and get a reference.

    Does this make sense?
     
  43. Saafir

    Saafir

    Joined:
    Apr 21, 2017
    Posts:
    4
    Hi, Mr. Adam
    I am stuck on the last part of Space Shooter , which is building and running the game. When I try to run the game it glitches meaning that the bottom screen moves off to the right in streak like manner. Can I send my code so you can review it?

    Glitch.PNG
     
  44. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The quicker solution might be to back-read this forum as I believe someone else had that issue and found a solution. If that doesn't work, post again?

    This doesn't really look like a code issue, but perhaps a setup issue?

     
  45. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Adam,

    This is the same person that posted it originally, so it would appear it has not been solved yet, and I would agree with Adam that it doesn't appear to be code related. It is well beyond my technical knowledge to just solve looking at the picture. I would offer two suggestions, maybe try posting in either the Unity Editor or graphics forum, or posting some type of file share that another person could test out the level and see if they could figure it out.

    Good luck with it.

    Edit: Definitely post your code. Be sure to use code tags.
     
  46. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you post more information such as the build target, machine os and version, etc., perhaps that would help us? More information about what you've done to try and solve it so we know what "doesn't work"? Screenshots of your project (do you have multiple cameras?) If you publish the "done" game, does it do the same thing? If you are publishing for the web, does it do this in every browser? If you are publishing for one target (eg: standalone) does it work if you try a different target (eg: web)??

    We need something to go on.
     
  47. techstyle

    techstyle

    Joined:
    Apr 28, 2017
    Posts:
    4
    Hi There. I'm completely stuck at the very last step of Shooting Shots. The new variables "Shot," "Shot Spawn," and "Fire Rate" are not showing up after I save my code. (The conversion to Unity 5 is killing me.) Can you take a look at this code and tell me what I'm possibly doing wrong?! Thanks so much.

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

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Do you have any errors in the console?

    If there are any errors, then Unity cannot complete the compile and the new fields will not show up.
     
  49. techstyle

    techstyle

    Joined:
    Apr 28, 2017
    Posts:
    4
    I found a capitalization error. (facepalm)
     
  50. SaI3rd

    SaI3rd

    Joined:
    May 10, 2017
    Posts:
    1
    Hello!

    I have got a problem when I placed the first asteroid.I have reset the position of the asteroid and made sure the positon Y was 0.But when I tested the game,the positon Y of the asteroid became 1 or even more.I cannot see my asteroid in my game and I think the shot cannot hit the asteroid either.Can you help me fix it?Thank you!