- Home /
Standard assets CarWayPointBased: how to let the vehicle drive and stop?
Hi guys,
I am new to Unity. When I am making use of CarWayPointBased, I set the "Driving" value to false because I don't want the car to move at the beginning.
I noticed there is a function called SetTarget() in CarAIControl script, so I cited it with a destination point as argument.
It seems the destination and m_Driving value has set correctly. I can see the wheels rotating with smoke behind the wheel. But the car does not move.
Additionally, if I set initial "Driving" value to true, the car does move. Problem is I want to stop it and restart it as I want.
Can anyone tell me what problem is it? I couldn't find any tutorials or user guide for this library...
It's easier to answer your question if you paste in the code that you've written, ins$$anonymous$$d of trying to describe it in words.
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using UnityStandardAssets.Utility;
namespace UnityStandardAssets.Vehicles.Car
{
[RequireComponent(typeof(CarController))]
[RequireComponent(typeof(CarAIControl))]
public class StopForLoading : $$anonymous$$onoBehaviour
{
public Transform testTarget;
private CarAIControl referencedController;
private WaypointProgressTracker referencedWaypointTracker;
private void Start()
{
referencedController = gameObject.GetComponent<CarAIControl>();
referencedWaypointTracker= gameObject.GetComponent<WaypointProgressTracker>();
StopTheCar();
Invoke("StartTheCar", 10);
}
private void StopTheCar()
{
referencedController.stopDriving();
}
private void StartTheCar()
{
referencedController.SetTarget(referencedWaypointTracker.target);
Debug.Log(referencedWaypointTracker.target.name);
}
}
}
What I am doing in the code is stop the car first, then in 10 seconds, start the car and let it move to the first point in the WayPointCircuit.
What stopDriving does is simply assign m_Driving in CarAIControl to false.
SetTarget is a function exists in CarAIControl.
I am having the exact same behavior. If I call SetTarget() in the Awake() function, the car takes off normally, but if I leave the Target as null, or mark m_Driving = false, and then call SetTarget() later (after my race countdown), the car stays in place and spins tires. It's like a parking brake is set, but I don't see anything like that.
$$anonymous$$eeping an eye on this thread for an answer/workaround. If I come up with one, will post solution.
Answer by SidDixit · Dec 19, 2015 at 08:08 AM
There's one more way to do this you make a new program of only calling functions of car. for eg - your program name is call.cs in which you have boolean values
move;
so in the car control program you may use
if(move==true)
//car should move//speed=100
else
//car stops//speed=0
i.e. you should control the cars speed....
i hope you have got your answer....
Answer by DanielleSisserman · Aug 12, 2018 at 10:38 AM
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;
}
@$$anonymous$$leSisserman Thank you posting! Was having a hard time finding out whats wrong with the start stop behaviour. Thanks for the "fix".