- Home /
Question by
NinjaRubberBand · Jan 10, 2014 at 01:19 PM ·
booleantrue
Boolean doesn't become true
I have this script, and it makes a car go forward and backwards. When i press w and it goes forward, and i release a car it checks a boolean called slowDown, and it works.
When i press s and it goes backward and release the key it does not check slowDownB Can someone see what i did wrong?
#pragma strict
var spin = 3;
var topSpeed = 20;
var speed : float = 0.0;
var slowDown = false;
var topSpeedB = -10;
var slowDownB = false;
function Update ()
{
//This to the next comment will be the car going forward.
if(Input.GetButton("FORWARD"))
{
speed = speed+0.2;
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
if (Input.GetButtonUp("FORWARD"))
{
slowDown = true;
}
if(speed > topSpeed)
{
speed = topSpeed;
}
if(slowDown == true)
{
speed = speed-0.2;
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
if(speed < 0)
{
speed = 0;
slowDown = false;
}
}
//This will be the car going backwards.
if(Input.GetButton("BACKWARD"))
{
speed = speed-0.2;
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
}
if (Input.GetButtonUp("BACKWARD"))
{
slowDownB = true;
}
if(speed < topSpeedB)
{
speed = topSpeedB;
}
if(slowDownB == true)
{
speed = speed+0.2;
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
if(speed > 0)
speed = 0;
slowDownB = false;
}
}
Comment
Answer by Graham-Dunnett · Jan 10, 2014 at 01:39 PM
Missing closing brace on line 75. Learn how to use the Debug.Log()
statement. ;-)
Answer by Sundar · Jan 10, 2014 at 01:44 PM
Your speed should be less than 0 not greater
if(slowDownB == true)
{
speed = speed+0.2;
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
if(speed < 0)
{
speed = 0;
slowDownB = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613130025im_/https://answers.unity.com/themes/thub/images/avi.jpg)