Search Unity

speed up the 2D endless runner game over time

Discussion in 'Scripting' started by taslima690, Oct 19, 2014.

  1. taslima690

    taslima690

    Joined:
    Sep 5, 2014
    Posts:
    5
    Hello. ..I am creating a endless runner game in 2D ..i want my character to spped up 5 ms after every 180 seconds ..but i couldn't do it .. i dont know why but it my character does not speed up ...i am new to unity and scripting..please help me...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour {
    5.  
    6.     public Vector2 speed = new Vector2(5, 5);
    7.     private Vector2 movement;
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         float inputX = Input.GetAxis("Horizontal");
    15.         float inputY = Input.GetAxis("Vertical");
    16.         if (Time.realtimeSinceStartup /5 == 0) {
    17.                         movement = new Vector2 (speed.x * inputX * 2, speed.y * inputY * 2);
    18.                         Debug.Log("yes..itsworking");
    19.                 } else {
    20.             movement = new Vector2(speed.x * inputX,speed.y * inputY);
    21.         }
    22.     }
    23.     void FixedUpdate()
    24.     {
    25.  
    26.         rigidbody2D.velocity = movement;
    27.     }
    28. }
    29.  
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Well this is how I would do it in your case
    Code (CSharp):
    1. float elapsedSeconds = 0;
    2.  
    3. void Update(){
    4. elapsedSeconds += time.deltaTime;
    5. if(elapsedSeconds > 180){
    6. elapsedSeconds = 0;
    7. //Insert speed up code here
    8.  
    9. //End speed up code
    10. }
    11. }
     
  3. taslima690

    taslima690

    Joined:
    Sep 5, 2014
    Posts:
    5
    Thank you very much for your reply but i still could not get it though ..i've write the code like this...can you help me out please...and thank you ...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour {
    5.  
    6.     public Vector2 speed = new Vector2(5, 5);
    7.     private Vector2 movement;
    8.  
    9.    
    10.  
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         float elapsedSeconds = 0;
    18.         float inputX = Input.GetAxis("Horizontal");
    19.         float inputY = Input.GetAxis("Vertical");
    20.         elapsedSeconds += Time.deltaTime;
    21.         if(elapsedSeconds > 180){
    22.             elapsedSeconds = 0;
    23.             //Insert speed up code here
    24.                 movement = new Vector2 (speed.x * inputX * 2, speed.y * inputY * 2);
    25.                 Debug.Log("yes..itsworking");
    26.  
    27.         }
    28.          else {
    29.             movement = new Vector2(speed.x * inputX,speed.y * inputY);
    30.         }
    31.         //End speed up code
    32.     }
    33.     void FixedUpdate()
    34.     {
    35.  
    36.         rigidbody2D.velocity = movement;
    37.     }
    38. }
    39.  
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Put "float elapsedSeconds = 0" under "private Vector2 movement"
     
  5. Tien-Tran

    Tien-Tran

    Joined:
    Nov 19, 2012
    Posts:
    2
    you can use Invoke
    Code (CSharp):
    1. void Start() {
    2.     //....
    3.  
    4.     // add this line
    5.     SpeedUp();
    6. }
    7.  
    8. void SpeedUp() {
    9.     // speed += 5;
    10.    Invoke ("SpeedUp", 180f);
    11. }
     
  6. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    How about using Coroutines?
     
  7. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246

    Your problem there is you declare the variable elapsedSeconds in Update. So every single frame, elapsedSeconds is set to 0 again. Do this instead:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour {
    5.  
    6.     public Vector2 speed = new Vector2(5, 5);
    7.     private Vector2 movement;
    8.     float elapsedSeconds = 0;
    9.  
    10.  
    11.  
    12.     void Start () {
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.         float inputX = Input.GetAxis("Horizontal");
    19.         float inputY = Input.GetAxis("Vertical");
    20.         elapsedSeconds += Time.deltaTime;
    21.         if(elapsedSeconds > 180){
    22.             elapsedSeconds = 0;
    23.             //Insert speed up code here
    24.                 movement = new Vector2 (speed.x * inputX * 2, speed.y * inputY * 2);
    25.                 Debug.Log("yes..itsworking");
    26.  
    27.         }
    28.          else {
    29.             movement = new Vector2(speed.x * inputX,speed.y * inputY);
    30.         }
    31.         //End speed up code
    32.     }
    33.     void FixedUpdate()
    34.     {
    35.  
    36.         rigidbody2D.velocity = movement;
    37.     }
    38. }
    39.  
     
    taslima690 likes this.
  8. taslima690

    taslima690

    Joined:
    Sep 5, 2014
    Posts:
    5
    Thank you ... Very Much ..Its working ...Finally ..
     
  9. taslima690

    taslima690

    Joined:
    Sep 5, 2014
    Posts:
    5
    Thank you ... Very Much ..Its working ...Finally ..
     
  10. taslima690

    taslima690

    Joined:
    Sep 5, 2014
    Posts:
    5
    Thanks everyone for support ... :)