- Home /
Question by
vmvenkat · Sep 03, 2015 at 06:52 AM ·
c#javascriptspeedcar
How to set speed limit of car?
I am developing a car game. When i press down arrow car get reverse faster like going forward. Here i want to set the reverse speed limit to 30mph. Anyone know about set speed limit of car?
Comment
Answer by hulahoolgames · Sep 03, 2015 at 07:09 AM
Can you post some code? Is there a reason why you are not just using an if statement to check the current value of car speed and increase it only if its less than 30? Something like this:
if(speed < 30.0f) {
// keep increasing the speed
}
Answer by huloo · Sep 03, 2015 at 07:16 AM
You can use Mathf.Clamp to clamp the speed :]
float clampedSpeed = Mathf.Clamp(yourSpeedValue, minSpeedLimit, maxSpeedLimit);
Answer by Naveen Kumar · Sep 03, 2015 at 08:11 AM
You Can set the speed limit to 30 when the key press down
void Update()
{
if (Input.GetKeyDown (KeyCode.DownArrow)) {
speed = Mathf.Clamp( speed, 0f, 30f);
}
}