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

Need Someone To Test This Script for Me

Discussion in '2D' started by Dogg, Jun 4, 2014.

?

Does it work?

  1. Yes

    0 vote(s)
    0.0%
  2. No

    0 vote(s)
    0.0%
  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I have a script, or three scripts that I need someone to test for me to see if the problem that is occurring with my script happens to another person. If you do want to test and use my code, I'll tell you what to look for. It's a powerup code that gives the player a speed boost and turns him invincible. All you have to do are these simple steps: Step 1. Attach the scripts to there designated places, Player Controller Script goes to the player, Collision Script goes on the Player, Powerup script goes on prefab powerup. Step 2. Check to see if the player speeds up and turns invincible. Step 3. Check to see if you can adjust the speed of how fast the player goes when he collects the powerup. Step 4. Check to see if you can adjust how long it takes for the powerup to wear off, like how long it takes to slow down and return to normal. Step 5. There is no step five. Really all I need is for someone to skip to step 4 and check to see if the powerup stops, but if you have time then go right ahead and do all the steps(AGAIN you don't have to). Anyways here's the scripts:

    This goes in player script:

    Code (CSharp):
    1. public float coeffSpeedUp = 2.5f;
    2.     public float coeffSpeedUpTime = 2.0f;
    3.     public float timer = 5;
    4.     public float invincibleTime = 5.0f;
    5.     [HideInInspector]
    6.     public bool isInvincible = false;
    7.  
    8. public void SetInvincible()
    9.     {
    10.                 isInvincible = true;
    11.                 renderer.material.color = Color.cyan;
    12.        
    13.         CancelInvoke ("SetDamageable"); // in case the method has already been invoked
    14.                 Invoke ("SetDamageable", invincibleTime = 5.0f);
    15.         }
    16.  
    17.     void SetDamageable()
    18.     {
    19.         isInvincible = false;
    20.         }
    21.  
    22. // Update is called once per frame
    23.     void FixedUpdate ()
    24.     {
    25.  
    26.                 transform.Translate (5f * coeffSpeedUp * Time.deltaTime, 0f, 0f);
    27.  
    28.  
    29. void Update()
    30.     {
    31.         if(timer > 5)      
    32.             timer -= Time.deltaTime;
    33.  
    34.         }
    35.         }
    36.  
    37.     public void SpeedUp()
    38.     {
    39.         // Speed up the player
    40.         coeffSpeedUp = 2.5f;
    41.  
    42.         CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
    43.         Invoke ("EndSpeedUp", coeffSpeedUpTime);
    44.     }
    45.    
    46.     void EndSpeedUp()
    47.     {
    48.         //Changes how fast the player goes
    49.         coeffSpeedUp = 5.0f; // back to normal
    50.     }
    51.  
    52. public IEnumerator StopSpeedUp() {
    53.  
    54.         Debug.Log( "StopSpeedUp()" );
    55.         yield return new WaitForSeconds(2.5f); // the number corresponds to the number of seconds the speed up will be applied
    56.         coeffSpeedUp = 1.0f; // back to normal
    57.         Debug.Log( "back to normal" );
    58.     }
    59. }
    60.  
    This goes in the Collider Script:

    Code (CSharp):
    1. void OnCollisionEnter2D (Collision2D col)
    2.     {
    3.         // If the colliding gameobject is an Enemy...
    4.         if(col.gameObject.tag == "Dangerous")
    5.         {
    6.             // check if player is NOT invincible
    7.             if ( !player.isInvincible ) // this is the same as writing if(player.isInvincible == false)
    8.             {
    9.                 // Find all of the colliders on the gameobject and set them all to be triggers.
    10.                 Collider2D[] cols = GetComponents<Collider2D>();
    11.                 foreach(Collider2D c in cols)
    12.                 {
    13.                     c.isTrigger = true;
    14.                 }
    15.                 // ... disable user Player Control script
    16.                 GetComponent<ControllerScript>().enabled = false;
    17.                 renderer.material.color = Color.gray;
    18.                
    19.             }
    20.             // else the player IS invincible, destroy all obstacles in the way
    21.             else
    22.             {
    23.                 // destroy the Dangerous object
    24.                 Destroy (col.gameObject); // This is to destroy the obstacles
    25.             }
    26.         }
    27.     }
    28. }
    29.  
    This goes in the powerup script:

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.                 if (other.tag == "Player") {
    4.                         ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); // not sure about the syntax here...
    5.                         if (playerScript) {
    6.  
    7.                                 // call function on playerScript to set player to invincible
    8.                                 playerScript.SetInvincible ();
    9.                                
    10.                                 // We speed up the player and then tell to stop after a few seconds
    11.                                 playerScript.SpeedUp();
    12.  
    13.                                 // We speed up the player and then tell to stop after a few seconds
    14.                                 playerScript.coeffSpeedUp = 2.5f;
    15.                                 StartCoroutine (playerScript.StopSpeedUp ());
    16.  
    17.  
    18.                         }
    19.                         Destroy (gameObject);
    20.                 }
    21.         }
    22.    
    23.     }
    24.  
     
  2. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    Doesn't seem to work right away, but I tweaked it a little, you can change the default speed, speedup speed and speedup time from inspector now. https://www.dropbox.com/s/6xty52f1o4h2oqj/TestMyScript.rar

    You don't need the Coroutine btw, since you're already stopping the speedup with Invoke.
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Thanks it fixed the problem I was having! But now I'm really slow before I grab the powerup. So like the default speed. I tried increasing the default speed but it didn't affect it at all. How to I change the default/regular speed of my character? Even your pill character goes really slow before getting the powerup. Here's my script:

    Code (CSharp):
    1. public float defaultSpeed = 20f;
    2.     //bool facingRight = true;
    3.     public float coeffSpeedUp = 2.5f;
    4.     public float coeffSpeedUpTime = 1f;
    5.     public float timer = 5;
    6.     public float invincibleTime = 5.0f;
    7.     [HideInInspector]
    8.     public bool isInvincible = false;
    9.     [HideInInspector]
    10.     public float speed = 20f;
    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.    
    21.     public void SetInvincible()
    22.     {
    23.         isInvincible = true;
    24.         renderer.material.color = Color.cyan;
    25.        
    26.         CancelInvoke ("SetDamageable"); // in case the method has already been invoked
    27.         Invoke ("SetDamageable", invincibleTime = 5.0f);
    28.     }
    29.    
    30.     void SetDamageable()
    31.     {
    32.         isInvincible = false;
    33.     }
    34.    
    35.     // Use this for initialization
    36.     void Start ()
    37.     {
    38.         anim = GetComponent<Animator>();
    39.        
    40.        
    41.     }
    42.    
    43.     // Update is called once per frame
    44.     void FixedUpdate ()
    45.     {
    46.        
    47.         transform.Translate (speed * Time.deltaTime, 0f, 0f);
    48.        
    49.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    50.         anim.SetBool ("Ground", grounded);
    51.        
    52.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    53.        
    54.         if (!grounded)
    55.             return;
    56.        
    57.         //float move = Input.GetAxis ("Horizontal");
    58.        
    59.         //anim.SetFloat ("Speed", Mathf.Abs(move));
    60.        
    61.         //rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    62.        
    63.     }
    64.    
    65.     void Update()
    66.     {
    67.         if(timer > 5)      
    68.             timer -= Time.deltaTime;
    69.  
    70.         if (grounded&&Input.GetKeyDown (KeyCode.Space))
    71.         {
    72.             anim.SetBool("Ground", false);
    73.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    74.            
    75.         }
    76.     }
    77.    
    78.     public void SpeedUp()
    79.     {
    80.         Debug.Log( "SpeedUp()" );
    81.         // Speed up the player
    82.         speed = defaultSpeed * coeffSpeedUp;
    83.        
    84.         CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
    85.         Invoke ("EndSpeedUp", coeffSpeedUpTime );
    86.        
    87.         //StartCoroutine (StopSpeedUp ());
    88.     }
    89.    
    90.     void EndSpeedUp()
    91.     {
    92.         //Changes how fast the player goes
    93.         Debug.Log ("ending");
    94.         speed = defaultSpeed; // back to normal
    95.     }
    96.    
    97.     //public IEnumerator StopSpeedUp() {
    98.        
    99.         //Debug.Log( "StopSpeedUp()" );
    100.         //yield return new WaitForSeconds(2.0f); // the number corresponds to the number of seconds the speed up will be applied
    101.         //coeffSpeedUp = 2.0f; // back to normal
    102.         //Debug.Log( "back to normal" );
    103.     }
    104. //}
    105.  
     
  4. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    Add
    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         speed = defaultSpeed;
    5.     }
    6.  
    to ControllerScript
     
    Dogg likes this.
  5. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Great it worked! Thank you so much for helping me.
     
  6. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Melang, sorry to bother you, but the script appears to not be working. Can you test it again when you have the time? If you do, check to see if you can increase the speedup time by 9. It does not go for that long for me, and I don't know why.:(
     
    Last edited: Jun 7, 2014
  7. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Never mind I figured it out.:)