# How do I slowly decrease a float from 0.5 to 0?

I have a 2D racing game with a track, and the track is repeated with vector2. When I die, I want to slowly stop moving the track, but I do not know how.

Here is my code:.

using UnityEngine;
using System.Collections;

public class trackMove : MonoBehaviour
{
public static trackMove Instance { get; private set; }

public float speed;
Vector2 offset;

// Use this for initialization
void Start ()
{
Instance = this;
}

// Update is called once per frame
void Update ()
{
offset = new Vector2(0, Time.time * speed);
GetComponent<Renderer> ().material.mainTextureOffset = offset;
}
}


Here is my current Inspector view:

You will have to make slight changes to the way you compute the offset and put additionnal code.

public float decreasingSpeed = 1;

private bool decreasing = false;
private float initialSpeed ;
private float stopTime = 0 ;

void Start ()
{
Instance = this;
}

void Update ()
{
if( decreasing )
speed = Mathf.SmoothStep( initialSpeed, 0, (Time.time - stopTime) * decreasingSpeed ) ;
offset = offset + new Vector2(0, Time.deltaTime * speed);
GetComponent<Renderer> ().material.mainTextureOffset = offset;
}

public void Stop()
{
decreasing = true;
initialSpeed = speed;
stopTime = Time.time ;
}

• Hello sir i wrote this code and i made decreasingSpeed = 0.1f; but my track it stop to move on 0.3 and from 0.3 to 0 it loop opposite. Commented Jul 3, 2017 at 21:17
• I don't understand your problem. I've tested the script I gave you, it works fine. You have to call the Stop function in order to make the track "decelerate". Make sure you don't disable the script while the track is decelerating. Commented Jul 4, 2017 at 7:11
• @Hellium: consider what happens if I've been driving forward for a minute, so my offset is fairly large. Now speed goes to zero, and my offset — which is calculated as assuming I've been traveling the same speed for the entire run of the game — also goes to zero (since zero times anything is zero). So the road not only slows down but goes backwards until I'm back at the starting line. This reverse will be more drastic the longer the game's been running — including time in menus and the like. This doesn't sound like intended behaviour. Commented Jul 4, 2017 at 12:11
• @DMGregory : You are right, I will change my code to avoid this issue. Commented Jul 4, 2017 at 13:05
• Great, thanks for the feedback @Driton ;) Don't forget to accept the answer for future readers. Commented Jul 4, 2017 at 17:10

What i've seen is , you got most of the logic right. What you need is just decrease speed to 0 when you die. Add a bool. Lets say;

public bool isPlayerDead = false;


Also you can add a float for decrease speed. Lets say;

public float decreaseSpeed = 0.01f;


In addition you can make one more bool to make the decreasion stop.

public bool isDecreasingDone = false;


When the player dies you turn this bool to true. So you can add the following in Update()

if(isPlayerDead == true && isDecreasingDone == false)
{
speed = speed - decreaseSpeed;
}

if(speed <= 0.0f)
{
speed= 0.0f;
isDecreasingDone = true;
}


You can edit the decreaseSpeed to change the slowing speed. This gives you a linear slowing.

Hope this helps! Cheers!

• This also needs a charge to the way the offset is calculated, or it will run into the same problem as Hellium's answer where the road reverses sharply, since the formula has no memory of progress made prior to stopping. Commented Jul 4, 2017 at 12:15