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

2d Invulnerable

Discussion in 'Scripting' started by simone9725, Mar 3, 2016.

  1. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Hi
    I ve got an issue with the player because it is invicible for an ammount of time but if i catch the shield powerup after the shield goes of the hp aren't substracted.Could someone help?
    Code (CSharp):
    1. //Called when the Trigger entered
    2.     void OnTriggerEnter2D(Collider2D other)
    3.     {
    4.         if (other.gameObject.tag == "Radioactive") // if we encounter a shield pickup...
    5.         {
    6.             invincible=true;
    7.             Destroy(other.gameObject);
    8.         }
    9.  
    10.         if (other.gameObject.tag == "Lama") // if we encounter a shield pickup...
    11.         {
    12.             if(!invincible)
    13.             {
    14.             HP--;
    15.             audio1.Play();
    16.         }
    17.         }
    18.         if (other.gameObject.tag == "Shield") // if we encounter a shield pickup...
    19.         {
    20.             shield.resetShield(); // reset shield durability to max
    21.             HeartsCount(); // draw the right number of hearts on the screen
    22.             shieldActive=true;
    23.             // Debug.Log(gameObject.name + " got shields!");
    24.             Destroy(other.gameObject); // and destroy the shield pickup object.
    25.         }
    26.         if (other.gameObject.tag == "DoubleShot") // if we encounter a shield pickup...
    27.         {
    28.             doubleShot=true;
    29.             Destroy(other.gameObject);
    30.         }
    31.         if (other.gameObject.tag == "LifePickup") // if we encounter a shield pickup...
    32.         {
    33.  
    34.             HP++;
    35.        
    36.             Destroy(other.gameObject);
    37.         }
    38.         if (other.gameObject.tag == "Boss") // if we encounter a shield pickup...
    39.         {
    40.             HP--;
    41.             audio1.Play();
    42.  
    43.         }
    44.         //Excute if the object tag was equal to one of these
    45.         if(other.tag == "Enemy" || other.tag == "Asteroid" || other.tag == "EnemyShot") // if we have no shields & we're hit by an enemy, bullet, or asteroid
    46.         {
    47.             if(!shieldActive)
    48.             if(!invincible) //shieldActive being the bool you used for checking if the shields are active
    49.            
    50.             {
    51.             HP--;
    52.             Destroy(other.gameObject);
    53.             Instantiate (Explosion, transform.position , transform.rotation);
    54.             // Debug.Log(gameObject.name + " has hit an " + other.gameObject.name);
    55.             HeartsCount(); // draw the right number of hearts on the screen
    56.                 if(!invincible)
    57.                 {
    58.             if (shield.lives == 0) // if we're out of shields, explode, self-destruct, and show the restart dialog.
    59.             {
    60.                 if (shield.lives == 0)
    61.                 {
    62.                     shieldActive=false;
    63.                     //HP--;
    64.                 }
    65.                 if(HP <0){
    66.                 // Debug.Log("Player has no shields left! Destroying player!");
    67.                 Instantiate (Explosion, transform.position , transform.rotation);
    68.                 //Trigger That its a GameOver
    69.                 Destroy(gameObject);
    70.                 restartDialog.SetActive(true);
    71.                     healthBar.enabled=false;
    72.             }else{
    73.                 GetComponent<Animator>().SetTrigger("Damage");
    74.             }
    75.         }
    76.     }
    77.     }
    78.     }
    79.     }
    80.     void HeartsCount()
    81.     {
    82.  
    83.         hits = shield.lives;
    84.         if(shield.lives == 3) // if we have full shields (3)...
    85.         {
    86.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #3.
    87.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #2.
    88.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
    89.         }
    90.         if(shield.lives == 2) // if shields are at 2...
    91.         {
    92.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
    93.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #2.
    94.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
    95.         }
    96.         if(shield.lives == 1) // if shields are at 1...
    97.         {
    98.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
    99.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #2.
    100.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
    101.         }
    102.         if(shield.lives == 0) // if shields are at 0...
    103.         {
    104.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
    105.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #2.
    106.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #1.
    107.         }
    108.     }
     
  2. pawfessor-fluff

    pawfessor-fluff

    Joined:
    Feb 7, 2016
    Posts:
    41
    Is the shieldActive flag on a timer too? Otherwise it looks like once it's set to true it remains like that (the body of the if at line 47 will always be skipped). The best way to figure this out is to set a breakpoint and examine the values of your flags.
     
  3. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Thanks for your reply how can I do this ?I haven't a timer
     
  4. pawfessor-fluff

    pawfessor-fluff

    Joined:
    Feb 7, 2016
    Posts:
    41
    If there's no timer then I don't see a place where you're reducing the lives of the shield. You probably want an else for the if at line 47, for example insert this after line 74:
    } else {
    shield.lives--;
    HeartsCount();
    }
    For the debugger have a look here: https://unity3d.com/learn/tutorials/modules/beginner/scripting/monodevelops-debugger
    Props to you if you're writing all that code without knowing how to use a debugger.
     
  5. Marikosama

    Marikosama

    Joined:
    Jun 26, 2015
    Posts:
    13
    if you want timer just use coroutine or invoke
    like

    public int shieldTime;
    int counterShieldTime;

    //when shield activated
    shieldTime = 0;
    InvokeRepeating("Timer",0,1);

    void Timer()
    {
    if(counterShieldTime != shieldTime)
    counterShieldTime++;
    else
    {
    //stop all invoke
    //deactivated shield
    }
    }
     
  6. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Sorry this is the shield lives manager
    Code (CSharp):
    1.  {
    2.         // enable the sprite renderer & collider 2d only when lives (durability) is greater than 0, otherwise disable them
    3.         if(lives <= 0)
    4.         {
    5.             sr.enabled = false;
    6.             col.enabled = false;
    7.  
    8.         }
    9.         if(lives > 0)
    10.         {
    11.             sr.enabled = true;
    12.             col.enabled = true;
    13.         }
    14.     }
    15.  
    16.     void OnTriggerEnter2D(Collider2D other)
    17.     {
    18.         if (lives <= 0 || col.enabled == false) // if lives (durability) is 0 or less, or the shields are disabled (same thing)...
    19.         {
    20.             return; // do nothing because shields should be off...
    21.  
    22.         }
    23.         if (lives >= 1 && (other.tag == "Enemy"|| other.tag == "Asteroid" || other.tag == "EnemyShot")) // if lives is greater than or equal to one & we're hit by an enemy, asteroid, or enemy bullet...
    24.         {
    25.  
    26.             lives--; // subtract one from lives counter
    27.  
    28.             // Debug.Log("Shields were hit by " + other.gameObject.name + ", lives (durability) is now " + lives + ".");
    29.             Destroy(other.gameObject); // destroy the other object
    30.         }
    31.     }
    32.     }
    The problem now is the when I collect shield the hp go down
     
  7. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Any ideas?
     
  8. pawfessor-fluff

    pawfessor-fluff

    Joined:
    Feb 7, 2016
    Posts:
    41
    Have you tried debugging? If you set a breakpoint at the start of your OnTriggerEnter you can trace line by line and see what's happening, as well as the actual value of the flags.
     
  9. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Well the problem seems to be that there isn't a contact .I can't see shield go
    Code (CSharp):
    1.    void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         Debug.Log ("shield go");
    4.         if (lives <= 0 || col.enabled == false) // if lives (durability) is 0 or less, or the shields are disabled (same thing)...
    5.         {
    6.             return; // do nothing because shields should be off...
    7.  
    8.         }
    9.         if (lives >= 1 && (other.tag == "Enemy"|| other.tag == "Asteroid" || other.tag == "EnemyShot")) // if lives is greater than or equal to one & we're hit by an enemy, asteroid, or enemy bullet...
    10.         {
    11.  
    12.             lives--; // subtract one from lives counter
    13.  
    14.             // Debug.Log("Shields were hit by " + other.gameObject.name + ", lives (durability) is now " + lives + ".");
    15.             Destroy(other.gameObject); // destroy the other object
    16.         }
    17.     }
     
  10. pawfessor-fluff

    pawfessor-fluff

    Joined:
    Feb 7, 2016
    Posts:
    41
    It may be that the collider is disabled, I see in your previous snippet you have a line which says col.enabled = false. Set a breakpoint there (or add some logging), as well as in the other places where you may be disabling the collider. It may be that the shield's lives go down to zero while invulnerable.