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);
}
}
}