huu tri
huu tri

Reputation: 58

Increase speed of an object until it reach a certain point then decrease it. C#

I am a newbie in C# language, I was try to make a simple game. The object starts with 0 speed. I want to increase the speed of an object gradually until it reach 1200, the moment it reach 1200 stop increasing and immediately decrease it speed gradually till it reach 0. But the function was in the update method, so I got stuck here. Could any one help me with this. I am much appreciated.

void Update()
    {
   // I got stuck here
      if(speed >= 0 && speed < 1200)
        {
            speed++;
        } 
        Debug.Log(speed);


    // this is where the speed was put in and the object start to move.
        if (spin)
        {
            foreach (Transform image in transform)
            {

                {
                    image.transform.Translate(Vector3.down * Time.smoothDeltaTime * speed, Space.World);

                    if (image.transform.position.y <= 0)
                    {
                        image.transform.position = new Vector3(image.transform.position.x, image.transform.position.y + 420, image.transform.position.z);
                    }
                }


            }

        }

Upvotes: 1

Views: 642

Answers (1)

Frenchy
Frenchy

Reputation: 17007

You could add a boolean var to help you:

private bool decrease = false;

void Update()
{
      if(speed >= 0)
      {
          if (speed == 1200)decrease = true;
          if (decrease)
             if (speed > 0) speed--;//dunno if you want to block at 0 when decreasing
          else
             speed++;
      } 
      Debug.Log(speed);

Upvotes: 2

Related Questions