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. Dismiss Notice

Invincible Script Won't Work Correctly?

Discussion in '2D' started by Dogg, May 17, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Hello, I have had some of this script made for me on the Unity answers, but it doesn't work correctly. When I grab the powerup and turn invincible, it stays invincible forever. It doesn't stop when it's supposed to stop(I have a certain time set).The person and I could not figure out what's wrong. So I decided to take things to the forums, since this is always the best place to go it seems, and I always get the help I need. Here's my scripts:

    Player Script:
    Code (csharp):
    1. public class ControllerScript : MonoBehaviour
    2. {
    3.     public float maxSpeed = 10f;
    4.     //bool facingRight = true;
    5.     public float coeffSpeedUp = 1100.5f;
    6.     public float coeffSpeedUpTime = 1.5f;
    7.     public float invincibleTime = 1.0f;
    8.     [HideInInspector]
    9.     public bool isInvincible = false;
    10.     public Renderer invincibleIndicator;
    11.  
    12.     Animator anim;
    13.  
    14.     bool grounded = false;
    15.     public Transform groundCheck;
    16.     float groundRadius = 0.2f;
    17.     public LayerMask whatIsGround;
    18.     public float jumpForce = 700f;
    19.  
    20.     //public void OnCollisionEnter2D (Collision2D col);
    21.  
    22.     public void SetInvincible()
    23.     {
    24.                 isInvincible = true;
    25.                 invincibleIndicator.enabled = true;
    26.  
    27.                 CancelInvoke ("SetDamageable"); // in case the method has already been invoked
    28.                 Invoke ("SetDamageable", invincibleTime);
    29.         }
    30.  
    31.     void SetDamageable()
    32.     {
    33.         isInvincible = true;
    34.         invincibleIndicator.enabled = false;
    35.         }
    36.  
    37.     // Use this for initialization
    38.     void Start ()
    39.     {
    40.         anim = GetComponent<Animator>();
    41.  
    42.         if ( !invincibleIndicator )
    43.         {
    44.             invincibleIndicator = transform.Find("InvincibleIndicator").renderer;
    45.         }
    46.        
    47.         invincibleIndicator.enabled = false;
    48.  
    49.     }
    50.    
    51.     // Update is called once per frame
    52.     void FixedUpdate ()
    53.     {
    54.  
    55.                 transform.Translate (5f * Time.deltaTime, 0f, 0f);
    56.  
    57.                 grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    58.                 anim.SetBool ("Ground", grounded);
    59.  
    60.                 anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    61.  
    62.                 if (!grounded)
    63.                         return;
    64.                        
    65.                 //float move = Input.GetAxis ("Horizontal");
    66.  
    67.                 //anim.SetFloat ("Speed", Mathf.Abs(move));
    68.  
    69.                 //rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    70.  
    71.     }
    72.  
    73.     void Update()
    74.     {
    75.  
    76.         if (grounded  Input.GetKeyDown (KeyCode.Space))
    77.         {
    78.             anim.SetBool("Ground", false);
    79.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    80.  
    81.         }
    82.     }
    83.  
    84.     public void SpeedUp()
    85.     {
    86.         // Speed up the player
    87.         coeffSpeedUp = 1.5f;
    88.        
    89.         CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
    90.         Invoke ("EndSpeedUp", coeffSpeedUpTime);
    91.     }
    92.    
    93.     void EndSpeedUp()
    94.     {
    95.         coeffSpeedUp = 1.0f; // back to normal
    96.     }
    97.  
    98.     public IEnumerator StopSpeedUp() {
    99.  
    100.         Debug.Log( "StopSpeedUp()" );
    101.         yield return new WaitForSeconds(1110.5f); // the number corresponds to the number of seconds the speed up will be applied
    102.         coeffSpeedUp = 1110.5f; // back to normal
    103.         Debug.Log( "back to normal" );
    104.     }
    105. }
    106.  
    Collision Script:
    Code (csharp):
    1. public class Collision : MonoBehaviour {
    2.  
    3. ControllerScript player;
    4.    
    5.     void Start()
    6.     {
    7.         player = GetComponent<ControllerScript>();
    8.     }
    9.    
    10.     void OnCollisionEnter2D (Collision2D col)
    11.     {
    12.         // If the colliding gameobject is an Enemy...
    13.         if(col.gameObject.tag == "Dangerous")
    14.         {
    15.             // check if player is NOT invincible
    16.             if ( !player.isInvincible ) // this is the same as writing if(player.isInvincible == false)
    17.             {
    18.                 // Find all of the colliders on the gameobject and set them all to be triggers.
    19.                 Collider2D[] cols = GetComponents<Collider2D>();
    20.                 foreach(Collider2D c in cols)
    21.                 {
    22.                     c.isTrigger = true;
    23.                 }
    24.                 // ... disable user Player Control script
    25.                 GetComponent<ControllerScript>().enabled = false;
    26.             }
    27.             // else the player IS invincible, destroy all obstacles in the way
    28.             else
    29.             {
    30.                 // destroy the Dangerous object
    31.                 Destroy (col.gameObject); // This is to destroy the obstacles
    32.             }
    33.         }
    34.     }
    35. }
    Powerup Script:
    Code (csharp):
    1. public class PowerupScript : MonoBehaviour {
    2.  
    3.  
    4.     void OnTriggerEnter2D(Collider2D other)
    5.     {
    6.                 if (other.tag == "Player") {
    7.                         ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); // not sure about the syntax here...
    8.                         if (playerScript) {
    9.  
    10.                                 // call function on playerScript to set player to invincible
    11.                                 playerScript.SetInvincible ();
    12.                                 // We speed up the player and then tell to stop after a few seconds
    13.                                 playerScript.SpeedUp();
    14.  
    15.                                 // We speed up the player and then tell to stop after a few seconds
    16.                                 //playerScript.coeffSpeedUp = 1110.5f;
    17.                                 //StartCoroutine (playerScript.StopSpeedUp ());
    18.  
    19.  
    20.                         }
    21.                         Destroy (gameObject);
    22.                 }
    23.         }
    24.    
    25.     }
    26.  
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Your "SetDamageable" function does the same thing as your "SetInvincible" function, instead of reversing the effect.

    Also, are you sure you can Invoke a private function? That seems like it shouldn't work.
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    So what should I do with the SetDamageable function? Remove it or set it to something else? As for your question, I'm pretty sure I can do that. If you think it's better to change it, please tell me so I won't have anymore problems.
     
  4. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    You should change it so that it sets "isInvincible = false;" instead of "isInvincible = true;" like you have now.


    Well, if the invincibleIndicator was being turned off correctly, then that's working. If it wasn't, try changing it to public. I don't really know the answer to this one; I prefer coroutines and direct calls.
     
  5. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I changed the isInvincible, it didn't affect anything. I still stay invincible forever, Just a note, when I get rid of all the invincibleIndicators, it allows me to collide with obstacles and die after about 4 seconds after getting the powerup. So I stay invincible for 4 seconds and then it turns off. I don't know why this is, but maybe that can help us figure out what's wrong.
     
  6. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I'm thinking of just dropping this little powerup of mine and updating it later on(since my skills lack). That is, if we can't figure this out.
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    When I was new to scripting (and to a certain extent still) I often encountered situations like this, whereby my programming skill at the time wasn't up to the job of implementing what I wanted. In the event that I couldn't eventually figure it out I either modified or omitted the feature I was trying to program.

    Then what usually happens is that a few months down the line something similar crops up and all of a sudden it's obvious how it's done, because of the programming knowledge you've gained along the way.

    I've also lost count of the times I've looked at old code (that at the time of was immensely proud of) and wondered what on earth possessed me to approach it in such a convoluted and inefficient way.

    Anyway I guess my point is don't get too hung up about a particular problem you can't solve, if it isn't game breaking, move or adapt and at some point you'll have learnt enough to implement it. And if it is game breaking, make a different game :mrgreen:
     
  8. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Got it, thanks for the advice. :)