- Home /
How to stop the car after the finish line?
hello, I'm working on a racing car game, I'm not getting how to stop the car after the car reaches the finish line. I've managed to stop the timer but I couldn't write a code to stop the car. Kindly help!!!1
Is your problem writing the code to make the code stop or is your problem communicating that the car has reached the goal to the car?
Answer by CodeMasterMike · Feb 06, 2013 at 06:15 AM
If you mean you want to stop the user input (acceleration, breaking, steering etc) from making the car move, then you need to put some kind of check to make sure that the player has reached the finish line:
if(PlayerHasReachedGoal == false)
{
// Check the player car input here...
}
If you mean that that the car should stop instantly without slowing down or using the brakes, then you should probably set the motortorque manually for all wheels to 0 every update:
void Update()
{
if(PlayerHasReachedGoal == true)
{
Wheel1.motorTorque = 0;
Wheel2.motorTorque = 0;
Wheel3.motorTorque = 0;
Wheel4.motorTorque = 0;
}
}
Worth mentioning is that I haven't used the car physics, so this above may be wrong, but it maybe gives you some idea how to stop the car without user input.
Good luck!
I prefer the 1s one. I tried to call the GetInput function using the if statement but it didnt work :( here's the code i used
if(myWC.GetGroundHit(hit)) { if(hit.collider.gameObject.tag == "finish") { GetInput(); }
Answer by falconer · Feb 06, 2013 at 11:53 AM
Thank you for that reply, I prefer the first one. But i'm not getting how to do it. Below is the code I've used to stop the timer after the car reaches the finish line. If anybody could add the instructions to stop the car would be very helpful.
var pastTime : float;
var myWC : WheelCollider;
private var isFinished : boolean = false;
var guiTime : GUIText;
function Update () {
var hit : WheelHit;
if(myWC.GetGroundHit(hit)) {
if(hit.collider.gameObject.tag == "finish") {
isFinished = true;
}
if(!isFinished) {
pastTime += Time.deltaTime;
}
guiTime.text = pastTime.ToString();
}
}
I named this as Timer and the input code is present in Car javascript
Ok, well, wherever the input code is, you need to add the "Is the player finished?" check, and if the player is not finished, then you proceed to check the input functions. If the player is finished, then you do not proceed to check the input functions.
You are great!!! one thing you missed was to make throttle=0...... Nevertheless you are my saviour :) Thank you so much..
Your answer
![](https://koobas.hobune.stream/wayback/20220613092229im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Nitro bar setup 1 Answer
RotateAround() using local rotation axes 2 Answers
How to make a pause function? 3 Answers
Wheel Collider General Friction 1 Answer
Stop object collision 3 Answers