Search Unity

Space Shooter Tutorial Q&A

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

  1. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    Space Shooter Part 3.01 - Audio

    Not sure if I'm doing something wrong or if the Upgrade Guide for this tutorial needs to be updated...

    The tutorial tells you to add asteroid and enemy explosion by either selecting the VFX Explosion prefab in the project window and then dragging the audio clip onto the prefab inspector, or by dragging the audio clip in the project window directly onto the prefab, also in the project window.

    Neither of these work for me. It instead creates an explosion_player (1) or explosion_asteroid (1) in the hierarchy window and attaches the audio clip to that rather than the actual prefab. The only way I could get it to attach to the prefab was to select the prefab, add component, audio, audio source, then drag the appropriate audio clip into the correct spot in the inspector.

    If it's me I'd like to know what I'm doing wrong. If not, I'm sure others would find it helpful to have the guide update.

    Thanks!
     
  2. ZTB

    ZTB

    Joined:
    Mar 16, 2017
    Posts:
    5
    Adding to my previous comment, I manually set the configuration to 600:900, will that work?
     
  3. Halo_Liberation

    Halo_Liberation

    Joined:
    Mar 6, 2017
    Posts:
    26
    I finally after 2 months finished the space shooter tutorial. But now i cant save it. When i try to it just makes a ton of files and the one that i assume plays the game doesnt work. But hey i at least got to the end after all this time. I do have a ton of people to thank for helping me understand my mistakes.
     
  4. Kainatic

    Kainatic

    Joined:
    May 31, 2017
    Posts:
    5
    Well, if anyone can help me understand my mistakes, I will be grateful to them. My problem is mentioned in post #2448 in this thread.
     
  5. BartekCompany

    BartekCompany

    Joined:
    May 11, 2017
    Posts:
    5
    build and run.png
    When I enter play mode, it works correctly without any errors. But when I want to build and run the game, there appear 26 errors mentioned above, that types couldn't be found, I don't know why. Scripts seem to be good.
     
  6. Meldiuz

    Meldiuz

    Joined:
    May 26, 2017
    Posts:
    9
    Well did solve my own issue after a while and the answer is easy just add
    using UnityEngine.SceneManagement; and that fixes it to bad that wasent in the finished code to see , ah well hope i helpt someone else as well :)
     
  7. Meldiuz

    Meldiuz

    Joined:
    May 26, 2017
    Posts:
    9
    Hello again for some reason unity freezes at the end wen i try to build the game so is there some work arround for this issue or maybe a setting that i have mist ? buildfreeze.jpg
     
  8. Meldiuz

    Meldiuz

    Joined:
    May 26, 2017
    Posts:
    9

    It tells you it's something about the namespace are they correctly named ? could be typos or what ever ! bicuse the error messages are complaning about your namespace ( quite new my self) but that's what i can make out of the info you have posted !
     
  9. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    Script looks good, same with your Asteroid prefab settings. I'm new to all this still myself so only suggestion I can make is to delete and rewrite the script and or literally go through that entire section of the tutorial again from the start and see if you catch whatever tiny thing was missed that is causing this. I had an issue in a previous tutorial that just didn't make any sense. I compared the script over but everything looked exactly the same but it was doing something very different from what it was supposed to. All I did was delete it, rewrite it, and it worked. I still don't know why.
     
  10. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    The console gives you 2 yellow errors about the whole Application.LoadLevel and Application.loadedLevel being obsolete but you can actually just ignore that. You don't need to use SceneManager. The game works just fine once you hit play and it will clear the errors out.

    It would have been nice if the tutorial had pointed that out though.
     
    Meldiuz likes this.
  11. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    I just finished the Ending the Game part, and everything works correctly.

    While the tutorial mentions that the Restart option doesn't appear until after the asteroid wave is cleared by the boundary destruction, it doesn't really explain why and I'm not understanding it from looking at the various scripts involved. It seems to me that as soon as the player ship is destroyed the script should be displaying the Restart text instead of waiting until after the asteroids are gone.

    Can someone explain why?
     
  12. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    I thought the same thing. Seemed like it froze at the same spot but I just left it alone and waited and it eventually finished building

    ETA: Seems this doesn't work in Chrome. Just the unity icons and a nice blank white screen...

    ETA #2: Seems that Unity has issues with practically all browsers and you may as well just not count on being able to build your game to a web platform. Really hope Unity is working bigtime on this as it seems a serious setback to its product.
     
    Last edited: Jun 4, 2017
    Meldiuz likes this.
  13. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    To start with, try setting the "Tag" to Enemy.

    Then, here are the asteroids from the "done" project:

    upload_2017-6-3_20-40-28.jpeg

    Note there are two scripts attached, RandonRotator and Mover

    upload_2017-6-3_20-41-0.jpeg
     

    Attached Files:

  14. Halo_Liberation

    Halo_Liberation

    Joined:
    Mar 6, 2017
    Posts:
    26
    I can redo the entire tutorial and compare my tumbling script to yours, i however always remove the "using System.Collections.Generic;" because i try to match the tutorial at all times unless needed with such cases being Rigidbody rb; being the exception.
     
    Last edited: Jun 4, 2017
  15. Meldiuz

    Meldiuz

    Joined:
    May 26, 2017
    Posts:
    9
    Ow okey i see thx for the information :) , il try building it again wen i get home then,
    and yes i hope they fix it too ^^
     
  16. Kainatic

    Kainatic

    Joined:
    May 31, 2017
    Posts:
    5
    I rewrote & re-did it like a dozen times but it still won't work. But, if you noticed the 'Edit' I did on my post, you will notice that I found the problem-maker was the 'Capsule Collider' which when I checked 'Is Trigger', solved the problem but in the video tutorial, the checking the 'Is Trigger' part was nowhere to be found. So, that's what confuses me.
     
  17. moconchap

    moconchap

    Joined:
    Jun 1, 2017
    Posts:
    5
    Hello I am having a little trouble with the build settings. In the tutorial he changed the build settings to Web Player but when I am bring that screen up on my own, it doesn't give me that option. Can I just go with PC, Mac, and Linux standalone or did I miss a step when importing or downloading? TIA.
     
  18. benedikt_20

    benedikt_20

    Joined:
    Jun 6, 2017
    Posts:
    2
    Heyy,
    as a complete noob I have some problems right at the beginning:

    For example, I could not find the platform "Web Player" in the build settings menu. (my currently Version is 5.6)
    Can anybody help me??
    Thanks in advance
     
  19. benedikt_20

    benedikt_20

    Joined:
    Jun 6, 2017
    Posts:
    2
    Heyy moconchap,

    after some Research I figured out, that the "Web Player" was removed in Version 5.4 so you would need to use a prior version of unity (e.g. 5.3). But I think it is not necessary to use the "Web Player " but you can go with PC, Mac, and Linux standalone and set the resolution manually (uncheck "Default is full screen").

    Please excuse potential language mistakes (I'm German)
     
    Faeryia and moconchap like this.
  20. Posunkoaa

    Posunkoaa

    Joined:
    Feb 26, 2017
    Posts:
    2
    Hello everybody! It would be great if you can help me with one bug in this game.

    When we create our spaceship enemies through Instantiate(..) function (which placed in Game Controller), it works perfect.
    But if we drop enemy prefab to the game scene (before game started), this enemy doesn't move along Z axis. It moves only along X.
    In first case (through Instantiate) script Mover (which sets Z speed) runs before script Evasive Maneuver.
    In the second case (drop prefab to scene) script Mover runs after script Evasive Maneuver. I checked it with Debug.Log(name of run script). Problem is HERE!
    And what I don't understand is why run order differs.
    Thanks a lot, it really breaks my brain.

    Sorry if my English is not good, it's not my mother tongue.
     
    Last edited: Jun 7, 2017
  21. Moonstar005

    Moonstar005

    Joined:
    Jun 11, 2017
    Posts:
    1
    The tutorial said to download assets, but I don't know where to download them. It may sound stupid, but this is my first time ever using Unity.
     
  22. UniKam

    UniKam

    Joined:
    Jun 9, 2017
    Posts:
    2
    Hi people!
    I have the same problem that this person so I will quote it here
    This person had no answer : http://answers.unity3d.com/questions/1194680/space-shooter-tutorial-mathfclamping-boundaries.html

    I will add that if I want to "leave" a boundary, the ship is, at first, "stuck" in the boundary until a certain point where I can move freely.

    Can you help me please?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Boundary
    7. {
    8.     public float xMin, xMax, zMin, zMax;
    9. }
    10.  
    11. public class PlayerController : MonoBehaviour
    12. {
    13.     private Rigidbody rb;
    14.     public int speed;
    15.     public Boundary boundary;
    16.  
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody>();
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         float moveHorizontal = Input.GetAxis("Horizontal");
    25.         float moveVertical = Input.GetAxis("Vertical");
    26.  
    27.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    28.         rb.AddForce(movement * speed);
    29.  
    30.         rb.position = new Vector3
    31.             (
    32.             Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
    33.             0.0f,
    34.             Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
    35.             );
    36.     }
    37. }
     
  23. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Perhaps change line 14 from int to float:
    from public int speed;
    to public float speed;

    also, in mine line 28 is
    rb.velocity = movement * speed;

    No idea if this is in fact the problem...
     
    UniKam likes this.
  24. UniKam

    UniKam

    Joined:
    Jun 9, 2017
    Posts:
    2
    Thank you for your answer!

    Just tested both solutions.
    Changing speed from int to float did not fix the problem.
    Using "velocity" instead of "AddForce" was the solution.

    It works close to perfectly now.
    The player is no longer "stuck" in the boundary.
    The player still crosses the boundary by 0.1 unit and then come back to it's position.

    I will search here for a fix.
     
    Last edited: Jun 13, 2017
  25. colinmennell

    colinmennell

    Joined:
    Jun 12, 2017
    Posts:
    2
    Hello

    I am having the exact same problem as this guy. Has anybody found an answer to this problem?
     
  26. StoobingTown

    StoobingTown

    Joined:
    Jun 8, 2017
    Posts:
    1
    Hi Guys,

    I have almost finished the Space Shooter Tutorial but cannot for the life of me figure out how to get the score to update. Everything is working perfectly except the score never goes above Score = 0. I have pasted my scripts for DestroyByContact and GameController below. Please help!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class DestroyByContact : MonoBehaviour {
    7.  
    8.     public GameObject explosion;
    9.     public GameObject playerExplosion;
    10.     public int scoreValue;
    11.     private GameController gameController;
    12.  
    13.     void Start ()
    14.     {
    15.         GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("Game Controller");
    16.         if (gameControllerObject != null)
    17.         {
    18.             gameController = gameControllerObject.GetComponent <GameController>();
    19.         }
    20.         if (gameController == null)
    21.         {
    22.             Debug.Log ("Cannot find 'GameController' script");
    23.         }
    24.  
    25.     }
    26.  
    27.     void OnTriggerEnter(Collider other)
    28.     {
    29.         if (other.tag == "Boundary")
    30.         {
    31.             return;
    32.         }
    33.         Instantiate (explosion, transform.position, transform.rotation);
    34.         if (other.tag == "Player")
    35.         {
    36.             Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
    37.         }
    38.  
    39.  
    40.         Destroy(other.gameObject);
    41.         Destroy(gameObject);
    42.         gameController.Addscore (scoreValue);
    43.     }
    44. }
    45.  
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    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 int score;
    16.  
    17.     void Start ()
    18.     {
    19.         score = 0;
    20.         UpdateScore ();
    21.         StartCoroutine (SpawnWaves ());
    22.     }
    23.  
    24.     IEnumerator SpawnWaves ()
    25.     {
    26.        
    27.         while (true)
    28.         {
    29.             yield return new WaitForSeconds (startWait);
    30.             for (int i = 0; i < hazardCount; i++)
    31.             {  
    32.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    33.                 Quaternion spawnRotation = Quaternion.identity;
    34.                 Instantiate (hazard, spawnPosition, spawnRotation);
    35.                 yield return new WaitForSeconds (spawnWait);
    36.  
    37.             }
    38.             yield return new WaitForSeconds (waveWait);
    39.         }
    40.  
    41.     }
    42.  
    43.     public void Addscore (int newScoreValue)
    44.     {
    45.         score += newScoreValue;
    46.         UpdateScore ();
    47.     }
    48.  
    49.  
    50.     void UpdateScore ()
    51.     {
    52.         scoreText.text = "Score: " + score;
    53.     }
    54.  
    55. }
    56.  
     
  27. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    perhaps line 43, capitalize score:
    public void AddScore (int newScoreValue)

    Ah no never mind, I see you are calling Addscore (small s) at line 42 of DestroyByContact.

    I would assure that scorveValue was non zero. Try one of the following things:
    1) Either temporarily add the line
    scoreValue=123;
    above line 42 of DestroyByContact to assure that it is set OR
    2) use debug log to see if it is set by adding
    Debug.Log ("scoreValue="+scoreValue);
    above line 42 of DestroyByContact




    If scoreValue is non zero,then add
    Debug.Log ("newScoreValue="+newScoreValue);
    to GameController above line 45

    and
    Debug.Log ("score="+score);
    to GameController above line 52

     
    Last edited: Jun 14, 2017
  28. colinmennell

    colinmennell

    Joined:
    Jun 12, 2017
    Posts:
    2
    Hey I have gotten stuck at the destroy by contact

    I am having the same trouble as this guy

    But when I destroy the asteroid and try again it does not fix the problem. Any thoughts as to why this may b happening?

    https://forum.unity3d.com/threads/space-shooter-tutorial-q-a.313899/page-2#post-2069505

    ""Hi all.

    I am currently working on this tutorial and I have a small issue in part 10.

    My bolts don't make contact with the asteroid. I mean they don't seem to trigger the OnTriggerEnter for destruction.

    I can see them go through the asteroid but without result.

    I checked and both the bolt and the asteroid collider are "is trigger".

    I also didn' have the boundary object and the asteroid disapearing like in the tuto. I guess those two issues are linked.

    Where should I look ?

    Edit : Well, problem solved. I deleted my asteroid Object and made a new one and everything is fine. I don't know where the problem came from, but I suspect the isTrigger of the asteroid. I think it was on in my previous attempt but it's off in the working one. I'll need to take a deeper look at what isTrigger do.""


    Edit.......

    I think that the problem might be coming from this line of code

    void OnTriggerEnter (Collider other)

    The word Collider does not change colour like in the example in the video
    I don't know why it wont change.

    I had the same problem setting up the Boundary. I could only get the boundary to work when I used the script Done_DestroyByBoundary from the done resources. Even if I would copy and paste the code into a new script it still would not work.

    My feeling is that for whatever reason the monodeveloper tool is not recognizing that word
    and then the collider is not properly set up.

    I have checked that IsTrigger is checked on both the bolt and Asteroid, and that there are no extra colliders or whatnot .


    here is the code as I wrote it

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

    public class destroyByContact : MonoBehaviour
    {
    void OnTriggerEnter (Collider other) {
    Destroy(other.gameObject);
    Destroy (gameObject);

    }

    }
     
    Last edited: Jun 13, 2017
  29. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    perhaps capitalize "d" in destroyByContact:
    public class DestroyByContact : MonoBehaviour
     
  30. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    Stoobing Town - I believe that you are missing parenthesis.

    your line is:
    scoreText.text = "Score: " + score;

    Should be:
    scoreText.text = ("Score: " + score);
     
  31. Barjack

    Barjack

    Joined:
    May 31, 2017
    Posts:
    3
    Hello!

    I'm new here.
    I followed the entire set of tutorials (but the one on mobile development) for the space shooter. Everything is clearly and fully explained. My game works fine and I've improved it with some basic ideas (getting/losing lives, invulnerability frames, power ups, difficulty increase, etc.)

    But, there's something I can't figure out how to deal with...
    When I move backward or forward (with or without horizontal direction) and keep shooting, my shots (bolts) are destroyed or canceled by something. I can't figure out if it's the player's ship collider or the other player's shots that were instantiated the frames before. I tried decreasing the speed of the shots (on the Mover script) and when I get over them with my ship (without shooting), they're being canceled (they disappear from the Hierarchy tab).
    I did google-fu but I can't find something...

    Sorry if the answer was already given and sorry for the bad english.
     
  32. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    I changed my bolt speed to 1 and tried to replicate your issue but my player ship does not explode when it comes in contact with its own bolts.

    Please post your DestroyByContact script, as that is most likely where the error is coming from.
     
  33. ashergarland

    ashergarland

    Joined:
    Jun 12, 2017
    Posts:
    4
    It appears that you are displaying an error each time you referenced a Unity class or method. This would indicate that you are not properly referencing the UnityEngine namespace (it also gives us the hint that we may be missing a namespace).

    Since this is not happening when you are developing and using play mode, this rules out the possibility of you simply missing the "using UnityEngine;" statement or any typo because the compiler would've caught that.

    The other possibility is that this is caused by the Unity engine .dll (a .dll is a file that basically contains all the unity engine code and is referenced by your game) not being properly built and exported along with the finished game. I would check your build settings and make sure you are using the correct build platform, which should be WebGL, and if that doesn't work then try to reinstall the WebGL Unity module.
     
    BartekCompany likes this.
  34. Barjack

    Barjack

    Joined:
    May 31, 2017
    Posts:
    3
    Hello! I didn't say that my ship explodes nor is destroyed. The bolts are being canceled when I fly through them or when I shoot while moving backward or forward.
     
  35. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    My apologies for misreading the issue you are having. It is still likely an error in the DestroyByContact script as that is the only thing that should be triggering the bolts to be destroyed. Without seeing the script it's hard to determine anything more specific
     
  36. Barjack

    Barjack

    Joined:
    May 31, 2017
    Posts:
    3

    Hello again!
    I figured out, with the help of a friend, what was the problem. As I expected, that was not something to do with the DestroyByContact behaviour script.

    The problem was pointed by the author of the turorials (Adam Buckner) at the end of this video (at 2:01:00) without going further to solve it).

    It's pretty simple. On the Chapter 1 Step 5 video, we've seen how to move the player's ship. To make it better, we've add a rotation when the ship goes right, left, up or down. On the next step, we've added shots that spawn depending on the ship's transform. So when the player goes up the ships angle aims the bottom of our scene and it aims the sky, when it goes down. So, with the Boundary seen on chapter 2 step 01, the bolts shot in direction of the sky or the ground are being destroyed (or cancelled).
    So when Adam Buckner shows us how to make a multiple shot on the video I linked above. They are destroyed the same way. The transform.rotation.x and transform.rotation.z should be set to 0.

    To correct the code showed by the author on this video, you should write something like that.

    foreach (var shotSpawn in shotSpawns)
    {
    Instantiate(shot, shotSpawn.position, shotSpawn.rotation = Quaternion.Euler(0, shotSpawn.rotation.y, 0));
    }

    Thanks for your answers and thanks to Adam Buckner for his videos.
     
  37. ultraroar101

    ultraroar101

    Joined:
    Jun 16, 2017
    Posts:
    2
    Hello,
    I am currently in the last stretch of the space shooter tutorial but apon watching the "Ending the game" tutorial I found that in Unity 5.6 the 'loadedlevel' function from
    "Application.LoadLevel (Application.loadedlevel);"
    is now obsolete and Unity no-longer contains a definition for it.
    Can I restart the level in a different way? or is there a way for me to get a definition for it?
     
  38. Joemt2011

    Joemt2011

    Joined:
    Jun 16, 2017
    Posts:
    2
    So I might be missing something very simple but i just can't figure it out. I'm working on the audio for the weapon, asteroid explosion and player explosion but whenever I play the game the two audio explosions happen in the first frame and don't follow up when the player and asteroid collide. I've followed the tutorial step by step but it doesn't look like i'm missing or forgetting anything.
     
  39. Joemt2011

    Joemt2011

    Joined:
    Jun 16, 2017
    Posts:
    2
    So I need help with the audio part of the tutorial. Whenever I start the game the player explosion and asteroid explosion go off in the first frame and I can't figure out why.
     
  40. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    Ignore the warning. While it says it is obsolete, it still works just fine
     
  41. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    Regarding the sounds playing immediately at the start - check the audio components in the inspector and make sure you have unchecked the box marked "Play On Awake".

    If your weapons audio isn't playing when you shoot, nor the explosions playing when asteroids/player explodes, I'd need you to post the code from the relevant scripts and possible a screenshot of the relevant components as well to compare to my own settings/code.
     
  42. moconchap

    moconchap

    Joined:
    Jun 1, 2017
    Posts:
    5
    thank you benedikt_20 I ended up just going a long with it and it worked out just fine. Still getting use to this system.
     
  43. hinovejas

    hinovejas

    Joined:
    Jun 17, 2017
    Posts:
    1
    Trying to work around this outdated guide is killing me. Does anyone just have a completed working script for Game Controller?
     
  44. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    The completed scripts are on the same page as all the video tutorials. If you'll say which tutorial video you're currently on and post your Game Controller script I'll take a look for errors.
     
  45. jsadusky

    jsadusky

    Joined:
    Jun 14, 2017
    Posts:
    1
    Hey guys, firstly, thanks for providing this tutorial. It's made jumping into Unity for the first time super breezy!
    My questions are more about fundamentals.
    1. We have a GameController to handle stuff the game itself is doing. We have a PlayerController to help the player ship. Yet instead of creating an EnemyController, we create a whole bunch of scripts - DestroyByCollision, WeaponController, Mover and EvasiveManeuver. Why don't we create an EnemyController, and have these various scripts listed as methods in it?
    2. When first setting up DestroyByCollision, it seemed weird to me to have that script be the one to destroy the player, and so early on I added a method to my player controller:

    void OnTriggerEnter(Collider other) {
    if(other.tag != "Boundary"){
    Instantiate (playerExplosion, transform.position, transform.rotation);
    Destroy (gameObject);
    gameController.GameOver ();
    };
    }​

    My thinking is that the PlayerController should be the one that decides what to do if it's hit by something. So far we're just blowing up, but if I wanted to add shields, power-ups, a healthbar, all of this would be a lot easier. Additionally, it seemed to make a lot of stuff later on easier. Is there anything I'm missing, where this is "bad code"?
     
  46. ZeRuc

    ZeRuc

    Joined:
    Mar 3, 2017
    Posts:
    25
    I want to make the enemy ships shoot bolts at an angle so that they go towards where the player ship is located, rather than merely going downwards. I have tried the following code (in WeaponController) but it doesn't work:

    Code (CSharp):
    1.     void Fire ()
    2.     {
    3.         GameObject player = GameObject.FindGameObjectWithTag("Player");
    4.         if (player != null)
    5.         {
    6.             Quaternion rot = Quaternion.FromToRotation(shotSpawn.position, player.transform.position);
    7.             Instantiate(shot, shotSpawn.position, rot);
    8.             GetComponent<AudioSource>().Play();
    9.         }
    10.     }
    Edit: It took me several hours, but I figured it out:

    Code (CSharp):
    1.                 Vector3 toFrom = player.transform.position - shotSpawn.position;
    2.                 int i = (player.transform.position.x > 0) ? -1 : 1;
    3.                 float angle = 180 + Vector3.Angle(Vector3.back, toFrom) * i;
    4.                 Quaternion rot = Quaternion.Euler(shotSpawn.rotation.x, angle, shotSpawn.rotation.z);
    5.                 Instantiate(shot, shotSpawn.position, rot);
    Would be even better if it would take into account player ship's velocity, but I guess that's too advanced coding for me :)
     
    Last edited: Jun 21, 2017
  47. VenkyXander

    VenkyXander

    Joined:
    Jun 20, 2017
    Posts:
    4
    My aspect ratio does not change to web after changing my resolution using player settings. Kindly help me 2017-06-20 (1).png
     
  48. The-Animus

    The-Animus

    Joined:
    May 28, 2017
    Posts:
    15
    In the Q&A of the "Extending Space Shooter" tutorial they change the Enemy ships dodge code so that it dodges toward the player. As far as I can tell I did everything just like the tutorial, and it works as intended, but it still gives me 400+ errors all saying more or less the same two things.

    "NullReferenceException: Object reference not set to an instance of an object
    EvasiveManeuver.Start () (at Assets/Scripts/EvasiveManeuver.cs:24)"

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

    Any help would be appreciated. Like I said the game still works, at least in the Unity environment, but it's annoying to have the console filled with that error, making it harder to see and deal with other errors.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EvasiveManeuver : MonoBehaviour
    7. {
    8.  
    9.     public float dodge;
    10.     public float smoothing;
    11.     public float tilt;
    12.     public Vector2 startWait;
    13.     public Vector2 maneuverTime;
    14.     public Vector2 maneuverWait;
    15.     public Boundary boundary;
    16.  
    17.     private Transform playerTransform;     // dodge towards player
    18.     private Rigidbody rb;
    19.     private float targetManeuver;
    20.     private float currentSpeed;
    21.  
    22.  
    23.     void Start ()
    24.     {
    25.         playerTransform = GameObject.FindGameObjectWithTag ("Player").transform; // dodge towards player
    26.         rb = GetComponent <Rigidbody> ();
    27.         currentSpeed = rb.velocity.z;
    28.         StartCoroutine (Evade ());
    29.     }
    30.  
    31.     IEnumerator Evade ()
    32.     {
    33.         yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y));
    34.  
    35.         while (true)
    36.         {
    37.             if (playerTransform != null)
    38.             {
    39.                 targetManeuver = playerTransform.position.x; // dodge towards player
    40.             }
    41.             //targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x);             //Remove the -Math if find better movement method
    42.  
    43.             yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y));
    44.             targetManeuver = 0;
    45.             yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y));
    46.         }
    47.     }
    48.  
    49.     void FixedUpdate ()
    50.     {
    51.         float newManeuver = Mathf.MoveTowards (rb.velocity.x, targetManeuver, Time.deltaTime * smoothing);
    52.         rb.velocity = new Vector3(newManeuver, 0.0f, currentSpeed);
    53.         rb.position = new Vector3
    54.             (
    55.                 Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
    56.                 0.0f,
    57.                 Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
    58.             );
    59.         rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
    60.     }
    61. }
    62.  
     
  49. drezzyatl

    drezzyatl

    Joined:
    May 25, 2017
    Posts:
    1
     
  50. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Can you confirm that your Player Gameobject in Unity does in fact have the tag set to "Player"

    If so, confirm in your code that it is being seen add lines 5-10 to your code
    Code (csharp):
    1.  
    2.  void Start ()
    3.  {
    4.  
    5. var pl=GameObject.FindGameObjectWithTag ("Player");
    6. if(pl == null)
    7. {
    8.    Debug.Log("Degug-YIKES: the player GameObject isnt found!!!");
    9.    return;
    10. }
    11.  
    12.  playerTransform = GameObject.FindGameObjectWithTag ("Player").transform; // dodge towards player
    13.  rb = GetComponent <Rigidbody> ();
    14.  currentSpeed = rb.velocity.z;
    15. StartCoroutine (Evade ());
    16. }
    17.  
    18.