Need Someone To Test This Script for Me

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:

public float coeffSpeedUp = 2.5f;
    public float coeffSpeedUpTime = 2.0f;
    public float timer = 5;
    public float invincibleTime = 5.0f;
    [HideInInspector]
    public bool isInvincible = false;

public void SetInvincible()
    {
                isInvincible = true;
                renderer.material.color = Color.cyan;
       
        CancelInvoke ("SetDamageable"); // in case the method has already been invoked
                Invoke ("SetDamageable", invincibleTime = 5.0f);
        }

    void SetDamageable()
    {
        isInvincible = false;
        }

// Update is called once per frame
    void FixedUpdate ()
    {

                transform.Translate (5f * coeffSpeedUp * Time.deltaTime, 0f, 0f);


void Update()
    {
        if(timer > 5)       
            timer -= Time.deltaTime;

        }
        }

    public void SpeedUp()
    {
        // Speed up the player
        coeffSpeedUp = 2.5f;

        CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
        Invoke ("EndSpeedUp", coeffSpeedUpTime);
    }
   
    void EndSpeedUp()
    {
        //Changes how fast the player goes
        coeffSpeedUp = 5.0f; // back to normal
    }

public IEnumerator StopSpeedUp() {

        Debug.Log( "StopSpeedUp()" );
        yield return new WaitForSeconds(2.5f); // the number corresponds to the number of seconds the speed up will be applied
        coeffSpeedUp = 1.0f; // back to normal
        Debug.Log( "back to normal" );
    }
}

This goes in the Collider Script:

void OnCollisionEnter2D (Collision2D col)
    {
        // If the colliding gameobject is an Enemy...
        if(col.gameObject.tag == "Dangerous")
        {
            // check if player is NOT invincible
            if ( !player.isInvincible ) // this is the same as writing if(player.isInvincible == false)
            {
                // Find all of the colliders on the gameobject and set them all to be triggers.
                Collider2D[] cols = GetComponents<Collider2D>();
                foreach(Collider2D c in cols)
                {
                    c.isTrigger = true;
                }
                // ... disable user Player Control script
                GetComponent<ControllerScript>().enabled = false;
                renderer.material.color = Color.gray;
               
            }
            // else the player IS invincible, destroy all obstacles in the way
            else
            {
                // destroy the Dangerous object
                Destroy (col.gameObject); // This is to destroy the obstacles
            }
        }
    }
}

This goes in the powerup script:

void OnTriggerEnter2D(Collider2D other)
    {
                if (other.tag == "Player") {
                        ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); // not sure about the syntax here...
                        if (playerScript) {

                                // call function on playerScript to set player to invincible
                                playerScript.SetInvincible ();
                               
                                // We speed up the player and then tell to stop after a few seconds
                                playerScript.SpeedUp();

                                // We speed up the player and then tell to stop after a few seconds
                                playerScript.coeffSpeedUp = 2.5f;
                                StartCoroutine (playerScript.StopSpeedUp ());


                        }
                        Destroy (gameObject);
                }
        }
   
    }

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. Dropbox - Error - Simplify your life

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

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:

public float defaultSpeed = 20f;
    //bool facingRight = true;
    public float coeffSpeedUp = 2.5f;
    public float coeffSpeedUpTime = 1f;
    public float timer = 5;
    public float invincibleTime = 5.0f;
    [HideInInspector]
    public bool isInvincible = false;
    [HideInInspector]
    public float speed = 20f;
   
    Animator anim;
   
    bool grounded = false;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;
    public float jumpForce = 700f;
   
   
    public void SetInvincible()
    {
        isInvincible = true;
        renderer.material.color = Color.cyan;
       
        CancelInvoke ("SetDamageable"); // in case the method has already been invoked
        Invoke ("SetDamageable", invincibleTime = 5.0f);
    }
   
    void SetDamageable()
    {
        isInvincible = false;
    }
   
    // Use this for initialization
    void Start ()
    {
        anim = GetComponent<Animator>();
       
       
    }
   
    // Update is called once per frame
    void FixedUpdate ()
    {
       
        transform.Translate (speed * Time.deltaTime, 0f, 0f);
       
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool ("Ground", grounded);
       
        anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
       
        if (!grounded)
            return;
       
        //float move = Input.GetAxis ("Horizontal");
       
        //anim.SetFloat ("Speed", Mathf.Abs(move));
       
        //rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
       
    }
   
    void Update()
    {
        if(timer > 5)       
            timer -= Time.deltaTime; 

        if (grounded&&Input.GetKeyDown (KeyCode.Space))
        {
            anim.SetBool("Ground", false);
            rigidbody2D.AddForce(new Vector2(0, jumpForce));
           
        }
    }
   
    public void SpeedUp()
    {
        Debug.Log( "SpeedUp()" );
        // Speed up the player
        speed = defaultSpeed * coeffSpeedUp;
       
        CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
        Invoke ("EndSpeedUp", coeffSpeedUpTime );
       
        //StartCoroutine (StopSpeedUp ());
    }
   
    void EndSpeedUp()
    {
        //Changes how fast the player goes
        Debug.Log ("ending");
        speed = defaultSpeed; // back to normal
    }
   
    //public IEnumerator StopSpeedUp() {
       
        //Debug.Log( "StopSpeedUp()" );
        //yield return new WaitForSeconds(2.0f); // the number corresponds to the number of seconds the speed up will be applied
        //coeffSpeedUp = 2.0f; // back to normal
        //Debug.Log( "back to normal" );
    }
//}

Add

void Start()
    {
        speed = defaultSpeed;
    }

to ControllerScript

1 Like

Great it worked! Thank you so much for helping me.

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.:frowning:

Never mind I figured it out.:slight_smile: