Im gonna answer this because I ren into the same issue. SidDixit solution will work. however, the car’s speed is read only and changing speed directly is not a smooth realistic stop.
another way is to set m_Driving = false;
looking at the FixedUpdate() in the AICarControll, it looks like this (i think…i did change the script a bit)
if (m_Target == null || !m_Driving)
{
// Car should not be moving,
// use handbrake to stop
m_CarController.Move(0, 0, 0f, 1f);
}
else
{
so, setting m_Driving = false is identical to using the brakes (fourth argument: handbrake = 1) in the user controlled prefab car.
if you do this, the car will stop. but, making it drive again is trickier than simply setting m_Driving to true again.
why?
If you played a little with the CarUserControl.cs (the other car prefab thats controlled by the input axis),
you’ve notice that if you make a full stop (using the spacebar or what ever your jump axis is) and than press the foward key (up arrow or w), the car wont go! its stuck! why? no clue…i personally consider it a bug.
the way to make the car go again is a tiny (even just one frame) negative acceleration (down arrow or s keys). and only then press the forward key.
we can do the same with the AICarControl. this is how I did it:
private void FixedUpdate()
{
if (m_Target == null || !m_Driving)
{
// Car should not be moving,
// use handbrake to stop
m_CarController.Move(0, 0, 0f, 1f);
}
else
{
......
......
......
if (m_CarController.CurrentSpeed < 0.002)
{
accel = -0.1f;
}
m_CarController.Move(steer, accel, accel, 0f);
......
.......
.......
and the actual stop and go functions are simply setting m_Driving:
void ReleaseBrakes()
{
m_Driving = true;
}
void HitBrakes()
{
m_Driving = false;
}