Nitro system

Hi, there was a problem in that when the nitro ends, the car still runs on nitro.

public Rigidbody Rb;

``````void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

else if (Input.GetKey(KeyCode.LeftShift))
{
Nitrous();
}

if (Input.GetKey(KeyCode.S))
{
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
}

if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
}

if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}

Lamp_key();

}

void Nitrous()
{
transform.Translate(Vector3.forward * nitroSpeed * Time.deltaTime);

StockNitro -= 1f * Time.deltaTime;

Debug.Log("Test" + StockNitro);

if (StockNitro <= 0f)
{
transform.Translate(Vector3.forward * 0f * Time.deltaTime);
//Rb.constraints = RigidbodyConstraints.FreezeAll; no work
}
}
``````

This line:

``````transform.Translate(Vector3.forward * 0f * Time.deltaTime);
``````

is completely useless since you do an active translate with a zero vector which will result in no motion whatsoever. However before you have this line unconditionally:

``````transform.Translate(Vector3.forward * nitroSpeed * Time.deltaTime);
``````

which will always execute when the “Nitrous()” method is executed. You probably want something like this:

``````void Nitrous()
{
if (StockNitro > 0f)
{
StockNitro -= 1f * Time.deltaTime;
Debug.Log("Test" + StockNitro);
transform.Translate(Vector3.forward * nitroSpeed * Time.deltaTime);
}
}
``````

`````` if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.LeftShift))
{
Nitrous();
}
``````

try

/////////////////////////////////

`````` if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
Debug.Log("Testing at normal speed." );
}
else if (Input.GetKey(KeyCode.LeftShift)&&Input.GetKey(KeyCode.W))
{
Nitrous();
}
``````

/////////////////////////////

`````` public float StockNitro =1f;

void Nitrous()
{
if (Equals(StockNitro, 0f))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
Debug.Log("Testing at normal speed because the nitrous canisters are Empty" );
} else {
StockNitro -= 0.1f * Time.deltaTime;
Debug.Log("Testing  with nitro" + StockNitro);
transform.Translate(Vector3.forward * moveSpeed +nitroSpeed * Time.deltaTime);
}
}
``````

otherwise: do this

because else if means to do this{} or this{} NOT if you can do both.

public float StockNitro =1f;

``````void Update(){
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
Debug.Log("Testing at normal speed." );
}
if (Input.GetKey(KeyCode.LeftShift))
{
Nitrous();
}
}

void Nitrous()
{
if (Equals(StockNitro, 0f))
{
Debug.Log("Testing at normal speed because the nitrous canisters are Empty" );
} else {
StockNitro -= 0.1f * Time.deltaTime;
Debug.Log("Testing  with nitro" + StockNitro);
transform.Translate(Vector3.forward * moveSpeed +nitroSpeed * Time.deltaTime);
}
}
``````