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

3 questions -2D - slow weapon projectiles / enemy not shooting after respawn / random time shooting

Discussion in 'Scripting' started by HolyNova, May 19, 2015.

  1. HolyNova

    HolyNova

    Joined:
    May 19, 2015
    Posts:
    13
    Hello, first of all, thanks for any help. I really appreciate it. I have few questions which I cant solve and I don't know what to do with them. Researched a lot, but with no good result. I hope, this forum would be able to help to move a bit. Thanks again for any help.

    1. I have made a new enemy script, where I shoot on player every 2 seconds, which works. The problem is, that is shoots very quickly (actually same speed of bullet as player). My goal is to shoot on player when he is alive, but shoot a bit slowly, so he can see the shot and have some time (let’s say 1 second) to evade it. I have no idea how to make shooting of enemies slower and longer. Googled yesterday for like 2 hours with no result :/ Does someone have an idea please? Here is my enemy AI http://pastebin.com/bPQUw8TR and enemy script for shooting http://pastebin.com/GgG36cJq

    2. Second problem is causing me some headaches too. Problem is that my player can shoot and kill the enemies and they can kill him, but when the player respawn (using GM script below), Enemies no longer shoot on my player game object. At least I don't see it. And after some while (like 15 second) of doing nothing, suddenly, player is dead and my game is lagging at that point horribly. I saw in profiler, that it is caused by A* path finding or something and it happens only with multiple enemies (like 15). I don't know why this behavior happens at all. Can someone give me a helping hand please?

    3. This is rather quick question. Right now, I use InvokeReapatition for enemies to shoot every 2 seconds, which makes them shoot every 2 second. But it makes all of them shoot at the same time even after respawn of the enemy. Is there a way, I can force them to shoot each in different time? I mean like that every enemy has his own timer, so they will be shooting separated and everyone in different time?

    Here are other scripts, which will be maybe needed:
    GM script for respawns and killing http://pastebin.com/ePidNbmH
    EnemySpawner script http://pastebin.com/m0VZFuMF
    Enemy class http://pastebin.com/wBCGMeiv
    Player class http://pastebin.com/His54ijV

    Thank you for any suggestions, even if you can help me with some part of question, please, do not hesitate to write it down. Thank you very much :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    1) raycast is immediate... you'll have to look at instantiating a shot so the player can see that shot and avoid it.
    2) how do the enemies know about the player? I don't see anything in those scripts where the pickup the player location? you respawn the player by destroy/instantiate, it may be that the existing enemies have a null value after the destroy and they aren't getting a reference to the new player transform?
    3) again, no code showing how the enemies fire... so. Sure, depending on how you've done it :p
     
  3. HolyNova

    HolyNova

    Joined:
    May 19, 2015
    Posts:
    13
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    bah, so you did :oops:

    so for 2) you set the playerrrr on Awake() in the enemy shooting script. If you destroy the player and instantiate another one the enemies aren't going to check for another player... you're going to need either an Update() on the enemies which checks for a null player and tries to find a new player object, or some sort of message when you respawn the player which tells the enemies who(or what) the new player object is and restart shooting etc.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    for 3) you are using

    Code (csharp):
    1.  
    2.  InvokeRepeating("Shoot", 2, 3);
    3.  
    so wait 2 seconds, call "Shoot" then every 3 seconds call "Shoot". You could change the 2 to
    Code (csharp):
    1.  
    2. Random.Range(minDelay, maxDelay)
    3.  
    so they all get a slightly different start time, but keep the 3 second fire rate
     
  6. HolyNova

    HolyNova

    Joined:
    May 19, 2015
    Posts:
    13
    3. Thanks for that tip. this works as I plan it. Will fiddle with it a bit more.
    2. Well the problem here is, that if I do this:

    Code (CSharp):
    1.  
    2. playerrrr = GameObject.FindGameObjectWithTag("Player");
    3.         if (playerrrr != null)
    4.         {
    5.            
    6.             firePoint = transform.FindChild("EnemyFP");
    7.             if (firePoint == null)
    8.             {
    9.                 Debug.LogError("No firepoint! Weapon or something missing bro");
    10.             }
    11.             InvokeRepeating("Shoot", Random.Range(minDelay, maxDelay), Random.Range(minDelay, maxDelay));
    12.  
    13.         }
    Then the enemies are not firing every random second, but the fire like crazy and player is dead instantly. If I am correct, its because it is in update method and therefore the invokerepetation method is called every frame. Is that right? So how can I do it that they fire just once?
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    something like...
    pseudo

    Code (csharp):
    1.  
    2. bool isShooting = false
    3.  
    4.  
    5. void function()
    6. {
    7. ...
    8. if(!isShooting)
    9. {
    10. invoke...
    11. isShooting = true;
    12. }
    13. }
    14.  
     
  8. HolyNova

    HolyNova

    Joined:
    May 19, 2015
    Posts:
    13
    That worked! And actually problem with enemies not shooting was in my method where I was calling new update for actual health. Which was a null object and after that, they just did not want to shoot. Thanks for you kind replay and help :)
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    no worries :D