- Home /
vehicle decceleration and angle reseting
I have this script for the back wheels of a car
function FixedUpdate () {
if(Input.GetKeyDown(KeyCode.W)) {
collider.motorTorque = 30; }
if(Input.GetKeyDown(KeyCode.S)) {
collider.motorTorque = -20; }
}
and this for the front wheels
function FixedUpdate () { if(Input.GetKey(KeyCode.A)) {
collider.steerAngle = -5; }
if(Input.GetKey(KeyCode.D)) {
collider.steerAngle = 5; }
}
How do i get the car to rather Constantly accelerate when i press a key or constantly turn, get it to to: deccelerate in the first script, and reset turning angle in the second?
thanks.
Answer by duck · May 19, 2010 at 07:54 PM
You might want to consider using GetAxis instead of reading the keys directly (since both WSAD and the arrow keys are mapped to the horizontal and vertical axes). The axis readings move smoothly from 0 to 1 or -1 when the keys are pressed, and back again when released.
So you could use something like this for your steering:
function FixedUpdate() {
var h = Input.GetAxis("Horizontal");
collider.steerAngle = h*5;
}
And like this for the motorised wheels:
function FixedUpdate() {
var v = Input.GetAxis("Vertical");
if (v > 0) {
collider.motorTorque = v*30;
} else {
collider.motorTorque = v*20;
}
}
You might find that your car naturally slows when cornering as an emergent behaviour from the wheel's slip and grip features.
Your answer
Follow this Question
Related Questions
Reduce steerangle at high speeds 1 Answer
Steering Overcompesation 0 Answers
Car Front Tires rotation clamped between 270° - 90° 0 Answers
how to add tilt steering to car? 2 Answers
Car steer problem 0 Answers