Driving, the very basics

Hello! Well, this is my first post here, and as I feel, not the last.

Right now, I am trying to get a basic hang of Unity. I tried a few times, and gave up, and returned when I had more energy for it. I always started various vehicle projects, as they seem easiest to learn the very basics with.

I followed this tutorial: http://www.gotow.net/andrew/blog/?page_id=78 and it gave some results. Some things were simply not explained though, assuming that the user already has the basic language/scripting knowledge. I dont have the first of the two, but, I’ve read references and followed his general method. I created a code or two, trying to keep it very simple, so that it doesnt quickly turn into spaghetti which I wont want to wade through. Right now I have two scripts, one for steering, applied to front wheels, one for torque, applied in the back.

//Steering:

var turningvalue = 10;

function Update () {
collider.steerAngle = Input.GetAxis("Horizontal") * turningvalue;
}
//Motor:

var addspeed = 1;

function Update() {
collider.motorTorque += Input.GetAxis("Vertical") * addspeed;
}

I pasted both, because both seem to be problematic. The steering one is an issue because the test car seems to be making a turn on its own, whereas I ensured that everything is perfectly symetrical. The motor script appears to run the car even without an input, but given some button holding, the car will still change the forward/back direction.

I’ll keep trying even as I wait for any help. Thank you in advance.

Welcome to the forum! Two things spring to mind as regards your script…

WheelColliders are physics components, and so they should be handled in FixedUpdate rather than Update. This goes for the steering and torque changes. Also, the reason the car keeps moving is that you are using the += operator to add torque. Just use = and you should be OK here.

I changed both steering and torque scripts to the FixedUpdate… It didnt help a lot because the car keeps behaving in a similar way. It sure does have an odd behaviour. When I add torque (ingame), the car slightly swerves out of the way, and when I release it, the car continues as normal. So the torque seems to cause the unwanted swerwing. However, after a while of driving around randomly, I stopped steering but it went into circles on its own all the time now. It probably still is. (yup it is) Where… does this difference occur? My best guess is that the back wheels (which have torque scripts attached) fall out of sync with each other somewhere, and run at different torque values.

I doublechecked my scripts. The steering script works just as I want it. During testing, I have my whole car selected, and the wheel colliders become visible, and I can watch them steer as intended. However, I cant be sure about the back wheels… as I am yet to figure out how to print variables onto the screen so that I can see them at all times… or some alternative of that.

Does this info suffice, or is there more that I should provide? At any rate, I’ll be trying in the meantime. Thank you for the response. And a warm welcome!

If you add public variables, assign values to them on each frame update and set the Inspector panel to Debug, you can see the values change in the inspector.

Is there any chance you could post the script that is causing the trouble? WheelColliders have a few subtleties, so it’s difficult to know exactly what the problem is here.

Here’s the script in its current form, slightly modified upon earlier script as advised. The steering script is still the same as in the first post, other that it now uses GetAxisRaw, for easier observation. No other scripts exist, so no other script could be making the problem.

function FixedUpdate() {
collider.motorTorque = Input.GetAxisRaw("Vertical") * 160;
}

I understand how this script works, it gives 160 torque to the engine, and multiplies it with the axis to determine whether it’s positive, negative, or zero. Again, to eliminate possible factors and complications, I used GetAxisRaw instead of GetAxis. There should be no problems with this script. The car still swerves at random (not so much left and right and back, but it sticks to one direction and uses that direction as default… whereas straight line should be default instead). After some thinking I assumed that the problem might be in the back propulsion, because back-propelled cars are most susceptible to the oversteer. But no, when I applied the script to just front wheels, which should create the most stable car setup, the car would still randomly change the default direction as before. So my assumption is that the issue is somewhere in the code (or the way it executes), and not in sensitive application of physics. Applying the script to all four wheels made the car noticeably, but still unacceptably little more stable.

At the same time, the car still wont stop! I understand why the += wouldnt stop the car, initially I wanted it so, but with this code the car still wont stop. I even added some main body linear drag, but it wont bring the car to a stop either. I even tried manually stopping while the game runs by hitting reverse button, but when I let the buttons go, the car still gains a small amount of speed and stays at that.

Is it possible that wheels can get stuck at different torques sometimes, even nonzero? Because letting the vertical axis go, they should reset to zero again. I havent checked those private variables yet, and how to watch them, as I dont really know the syntaxes to them, or how to really use them…

Well, as I said earlier, I’ll keep trying stuff even while I wait for the answer, though I ran out of ideas for now. Thank you for the advices.