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

How To Create Boost For Player After Collecting Powerup

Discussion in 'Scripting' started by Dogg, May 23, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Hello, it's my first time posting in the scripting thread. I need some help with creating this boost powerup. I've already created a script but it doesn't speed up my player at all, or at least it doesn't look like it. I don't want the speed to gradually increase, I want a boom effect. Take a look at this video: http://www.youtube.com/watch?v=-d9A2oq1N38 Skip to 1:36. When I collect the powerup, I want my player to stop when he grabs the powerup, play an animation, and then blast off. You don't need to help me with the animation and stop time(though I wouldn't mind), but just the speed up part because that's the whole point of this thread. I was thinking I need to use ForceMode.VelocityChange, but I don't know how to do that(I'm currently experimenting with it). Please help, here's my script:

    Code (csharp):
    1. public class BoostPowerupScript : 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.  
    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 = 6.0f;
    8.     [HideInInspector]
    9.     public bool isInvincible = false;
    10.  
    11.     Animator anim;
    12.  
    13.     bool grounded = false;
    14.     public Transform groundCheck;
    15.     float groundRadius = 0.2f;
    16.     public LayerMask whatIsGround;
    17.     public float jumpForce = 700f;
    18.    
    19.  
    20.     public void SetInvincible()
    21.     {
    22.                 isInvincible = true;
    23.  
    24.                 CancelInvoke ("SetDamageable"); // in case the method has already been invoked
    25.                 Invoke ("SetDamageable", invincibleTime);
    26.         }
    27.  
    28.     void SetDamageable()
    29.     {
    30.         isInvincible = false;
    31.         }
    32.  
    33.     // Use this for initialization
    34.     void Start ()
    35.     {
    36.         anim = GetComponent<Animator>();
    37.  
    38.    
    39.     }
    40.    
    41.     // Update is called once per frame
    42.     void FixedUpdate ()
    43.     {
    44.  
    45.                 transform.Translate (5f * Time.deltaTime, 0f, 0f);
    46.  
    47.                 grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    48.                 anim.SetBool ("Ground", grounded);
    49.  
    50.                 anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    51.  
    52.                 if (!grounded)
    53.                         return;
    54.                        
    55.                 //float move = Input.GetAxis ("Horizontal");
    56.  
    57.                 //anim.SetFloat ("Speed", Mathf.Abs(move));
    58.  
    59.                 //rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    60.  
    61.     }
    62.  
    63.     void Update()
    64.     {
    65.  
    66.         if (grounded  Input.GetKeyDown (KeyCode.Space))
    67.         {
    68.             anim.SetBool("Ground", false);
    69.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    70.  
    71.         }
    72.     }
    73.  
    74.     public void SpeedUp()
    75.     {
    76.         // Speed up the player
    77.         coeffSpeedUp = 1.5f;
    78.        
    79.         CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
    80.         Invoke ("EndSpeedUp", coeffSpeedUpTime);
    81.     }
    82.    
    83.     void EndSpeedUp()
    84.     {
    85.         coeffSpeedUp = 1.0f; // back to normal
    86.     }
    87.  
    88.     public IEnumerator StopSpeedUp() {
    89.  
    90.         Debug.Log( "StopSpeedUp()" );
    91.         yield return new WaitForSeconds(1110.5f); // the number corresponds to the number of seconds the speed up will be applied
    92.         coeffSpeedUp = 1110.5f; // back to normal
    93.         Debug.Log( "back to normal" );
    94.     }
    95. }
    96.  
     
  2. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    Within your FixedUpdate method, change the translate call to something like the following and it should take into account the speed multiplier.

    Code (csharp):
    1.  
    2.  
    3. transform.Translate (5f * coeffSpeedUp * Time.deltaTime, 0f, 0f);
    4.  
    5.  
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Thanks a lot it finally works! Haha and now when I grab the powerup, speed up, and jump, I go so fast I don't even let the level load correctly(There's like a gap/death hole because the terrain hasn't loaded yet). If the invincibility in my powerup actually worked correctly(it doesn't stay invincible for the time I want it to), then I would probably fall off the terrains spawning and die :D. I'll have to fix that. Thanks again. By the way, where did you learn how to do things like this? I would of never guessed to edit the translate call.
     
  4. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Time to bother, sorry. Hello again Mr.lorenalexm. I am reviving this old thread and talking to you again because I have a problem with the speed boost you helped me with. I really don't think you would know about this, but my speedup time does not work. It's my fault for not properly testing the script, so don't worry, but hey at least I caught it. Could you take a look at my code again and see what exactly is wrong? I would update the script in my first post but I need it for old reference. So here's a new one:

    Code (CSharp):
    1. public float coeffSpeedUp = 2.5f;
    2. public float coeffSpeedUpTime = 2.0f;
    3.  
    4. // Update is called once per frame
    5.     void FixedUpdate ()
    6.     {
    7.  
    8.                 transform.Translate (5f * coeffSpeedUp * Time.deltaTime, 0f, 0f);
    9. }
    10.  
    11. public void SpeedUp()
    12.     {
    13.         // Speed up the player
    14.         coeffSpeedUp = 2.5f;
    15.    
    16.         CancelInvoke ("EndSpeedUp"); // in case the method has already been invoked
    17.         Invoke ("EndSpeedUp", coeffSpeedUpTime = 1.0f);
    18.  
    19. void EndSpeedUp()
    20.     {
    21.         //Changes how fast the player goes
    22.         coeffSpeedUp = 5.0f; // back to normal
    23.     }
    24.  
    25. public IEnumerator StopSpeedUp() {
    26.  
    27.         Debug.Log( "StopSpeedUp()" );
    28.         yield return new WaitForSeconds(2.0f); // the number corresponds to the number of seconds the speed up will be applied
    29.         coeffSpeedUp = 2.0f; // back to normal
    30.         Debug.Log( "back to normal" );
    31.     }
    32. }
    As you can see, first there's coeffSpeedUp, which controls how fast the player goes. Then there's coeffSpeedUpTime, which at first I thought controlled the duration of the speed up(how long it lasts) but it turns out it controls how fast the speed up takes place. For an example, if I set it to 10.0, it would take 10 seconds for the speed up to take place. So the only one that should stop the speed up is StopSpeedUp, but as you can see it is set for 2.0 seconds but it never ends, literally. You didn't create this code so I'm not expecting much from you, but I think you and others on this great forum can help me.
     
    Last edited: Jun 4, 2014
  5. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    @Dogg instead of adding all of these extra methods into your class and having to worry about invoking calls and coroutines you could attempt something similar to the following. This avoids the clutter, in my opinion, by only needing one additional method than what is already provided by MonoBehaviour and from what I am reading achieves the same functionality.

    Code (CSharp):
    1. public float coeffSpeed = 5.0f;
    2. public float coeffSpeedMultiplier = 2.0f;
    3. public float coeffSpeedDuration = 5.0f;
    4.  
    5. private float currentSpeed = 5.0f;
    6. private float multiplierStartTime = 0.0f;
    7.  
    8. private void Update()
    9. {
    10.     // Check if duration has elapsed
    11.     if(Time.time > (multiplierStartTime + coeffSpeedDuration))
    12.     {
    13.         // Reset speed
    14.         currentSpeed = coeffSpeed;
    15.     }
    16. }
    17.  
    18. private void FixedUpdate()
    19. {
    20.     // Translate object forward multiplied by its current speed
    21.     transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
    22. }
    23.  
    24. private void OnSpeedPickup()
    25. {
    26.     // Reset start time
    27.     multiplierStartTime = Time.time;
    28.  
    29.     // Set current speed
    30.     currentSpeed = coeffSpeed * coeffSpeedMultiplier;
    31. }