- Home /
car movement basics
[Solved]1. I got a car that I've made and a script I found that I've tweaked a little, but the problem is that the car isnt doing what I want it to.
It is fairly easy to handle at low speeds but if it goes a little faster than that it flips as soon as I turn a little to one side.
I read somewhere about changing gravity might fix it but I dunno how.
also, the turning speed is like same as the torque which isnt what I want, fast turning and maby not as fast for moving.(should be changeable)
2.Now the car turns right when I try to go forward in a straight line and left when I go backwards,
code:
if(speed > maxSpeed){
RearLeftWheel.motorTorque = 0;
RearRightWheel.motorTorque = 0;
}else{
if(speed != 0)
throttle = Input.GetAxis("Vertical");
else
throttle = Input.GetAxis("Vertical") - 0.3;
FrontLeftWheel.motorTorque = EngineTorque * throttle;
FrontRightWheel.motorTorque = EngineTorque * throttle;
//I tried fixing with this but didnt help
FrontLeftWheel.motorTorque = FrontRightWheel.motorTorque;
FrontRightWheel.motorTorque = FrontLeftWheel.motorTorque;
}
FrontLeftWheel.steerAngle = 20 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 20 * Input.GetAxis("Horizontal");
EDIT: Use This Demo to see how the car behaves if it helps(Tip, use F to see each wheels rpm)
I find the turning most noticable at low speeds too.
Answer by Chris12345 · Jun 25, 2012 at 02:31 PM
Put this on your car and place script in a java script. and add a rigged body to you car setup and you are done. hope this works for you.
var rearWheel1 : WheelCollider;
var rearWheel2 : WheelCollider;
var frontWheel1 : WheelCollider;
var frontWheel2 : WheelCollider;
var wheelFL : Transform;
var wheelFR : Transform;
var wheelML : Transform;
var wheelMR : Transform;
var wheelRL : Transform;
var wheelRR : Transform;
var steer_max = 20;
var motor_max = 40;
var brake_max = 100;
var steerSpeed = 20;
private var steer = 0;
private var forward = 0;
private var back = 0;
private var brakeRelease = false;
private var motor = 0;
private var brake = 0;
private var reverse = false;
private var speed = 0;
function Start() {
rigidbody.centerOfMass = Vector3(0, -0.05, 0);
}
function FixedUpdate () {
speed = rigidbody.velocity.sqrMagnitude;
steer = Input.GetAxis("Horizontal");
forward = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1);
back = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);
if(speed == 0 && forward == 0 && back == 0) {
brakeRelease = true;
}
if(speed == 0 && brakeRelease) {
if(back > 0) { reverse = true; }
if(forward > 0) { reverse = false; }
}
if(reverse) {
motor = -1 * back;
brake = forward;
} else {
motor = forward;
brake = back;
}
if (brake > 0 ) { brakeRelease = false; };
rearWheel1.motorTorque = motor_max * motor;
rearWheel2.motorTorque = motor_max * motor;
rearWheel1.brakeTorque = brake_max * brake;
rearWheel2.brakeTorque = brake_max * brake;
if ( steer == 0 && frontWheel1.steerAngle != 0) {
if (Mathf.Abs(frontWheel1.steerAngle) <= (steerSpeed * Time.deltaTime)) {
frontWheel1.steerAngle = 0;
} else if (frontWheel1.steerAngle > 0) {
frontWheel1.steerAngle = frontWheel1.steerAngle - (steerSpeed * Time.deltaTime);
} else {
frontWheel1.steerAngle = frontWheel1.steerAngle + (steerSpeed * Time.deltaTime);
}
} else {
frontWheel1.steerAngle = frontWheel1.steerAngle + (steer * steerSpeed * Time.deltaTime);
if (frontWheel1.steerAngle > steer_max) { frontWheel1.steerAngle = steer_max; }
if (frontWheel1.steerAngle < -1 * steer_max) { frontWheel1.steerAngle = -1 * steer_max; }
}
frontWheel2.steerAngle = frontWheel1.steerAngle;
wheelFL.localEulerAngles.y = frontWheel1.steerAngle;
wheelFR.localEulerAngles.y = frontWheel2.steerAngle;
wheelFR.Rotate(0, 0, frontWheel1.rpm * -6 * Time.deltaTime);
wheelFL.Rotate(0, 0, frontWheel2.rpm * -6 * Time.deltaTime);
wheelMR.Rotate(0, 0, rearWheel1.rpm * -6 * Time.deltaTime);
wheelML.Rotate(0, 0, rearWheel2.rpm * -6 * Time.deltaTime);
wheelRR.Rotate(0, 0, rearWheel1.rpm * -6 * Time.deltaTime);
wheelRL.Rotate(0, 0, rearWheel2.rpm * -6 * Time.deltaTime);
}
Well thanks for your answer but I'd like to stay with the code I have and the wheels should turn around the x axis cuz they are weirdly turned and I have no idea how to fix that, I have tried with changins steerangle which only turns on Y axis to transform.rotation.x but they are still turned wrong way. The code I had before used this line:
transform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler(0, CorrespondingCollider.steerAngle + 90,RotationValue);
Try changing your wheel collider rotation. the wheel operates off of the in order to work.
Cars can be purchased online now, gone are the days when the buyer use to visits several showrooms to purchase cars and they were very expensive, due to online selling the cost of setting up showroom is saved and hence the seller is willing to sell the car in low price. 車査定
@Chris12345 I tried but the wheels themselves turns back to being horizontally ins$$anonymous$$d of vertically. I'd really like to know whats wrong with my other code.
EDIT: I fixed the turning at low speeds with setting all wheels motortorque to the front left wheel but it seems like the Input.getAxis("Vertical") is stuck after the car goes over about 60-80km/h(speed = FrontLeftWheel.rpm * 0.07619;)
Answer by crogers · Jun 25, 2012 at 07:37 PM
Yes, the wheels need to be "pointed" in the correct direction. If the car is spinning, left wheels are not going the same direction as the right wheels. I wouldnt bother with trying to rotate the geometry wheels until all of the physics are correct.
It's easy to have way too much torque, balancing the mass and tork is important to get realistic effects.
The trick listed above to lower the center of gravity is very important, otherwise your center of gravity is the geometric center of your collider. A real car is very dense at the botom....
Also, when you drive at a high speed in real life, the centrifugal force of the wheels makes it tough to steer and adds stability (like a bicycle). In my driving game, I make steering less effective at high speeds, this decreases the car flipping.
I have fixed the turning at low speeds already and no need to fix at high speeds cause the car is going fast enough. But the problem is that the throttle gets stuck at over about 60km/h.
Input.GetAxis("Vertical");
Answer by Chris12345 · Jun 23, 2012 at 01:45 PM
You may have too much EngineRPM and EngineTorque also if you have a rigidbody you should set the mass to 1500 that is a normal cars mass.
also make sure that your centre of mass is a empty game object and is placed in the centre of the car.
Ah, I fixed the flipping just with making a new clean script without the gear thingy. But now I have a new problem, the car like turns when I put some throttle on. I checked each tires rpm and it looks like the right sides wheels are turning faster when I start from stand still. Same with when I go backwards.
its weird cuz they are both controlled by this:
FrontLeftWheel.motorTorque = EngineTorque * throttle;
FrontRightWheel.motorTorque = EngineTorque * throttle;
You will need to make sure your wheels all line up perfectly too, double check each colliders properties also.
I checked and double checked, all lined up and same settings. I just cant believe how it can behave like this. Theres nothing that interfears with them exept that the wheels are "inside" of the car, which havent caused any trouble yet.
I also found out that the rear wheels get some sort of random rotation that make them turn a little, have tried a few ways but so far havent found a way to keep them still...
Answer by pudd1nG · Jun 17, 2012 at 10:26 PM
Well you can change the gravity in Edit->Project->Physics but I don't think that's what you need to do. Try to lower the torque of the wheels by adding mass to the rigidbody, i would also adjust drag to suit and get rid of angular drag to stop the ice skating feeling.
What I like to do with cars is actually apply a downward force to the rigidbody (to act as wind downforce). The Unity wheel colliders seem to the like this too and if you adjust the center of gravity with this force when you turn you can get some really nice effects (and handling) when turning.
Good luck! Best advice I can give is think about a real car, and what unity physics you can apply to your rigidcar to emulate.
Thanks for your reply but some code for the downforce drag since I don't know why it wont work for me.
The driving itself doesnt have to be like a real car since Its only gonna be like a bot/tank and doesnt need that nice effects other than driveable and manouverable. By the way how do I get the speed of the car and maby set a maxspeed??
Answer by Chuck 1 · Jun 27, 2012 at 03:49 PM
Not sure if it will help, but the front wheels on real cars are turned very slightly inward at the front. This is called "toe in" and is the reason why your steering wheel straightens out when you're moving forward and let the wheel turn on its own. That might help with the steering drift problem you're having. I couldn't get the car to flip when I turned at up to 60mph - but I did some sweet skids. Honestly the best five minutes of my day so far.
Your answer
Follow this Question
Related Questions
Horizontal axis not working after animation 1 Answer
Flip over car after tips over? 1 Answer
Player launches into air when hitting steep slopes 0 Answers
How do I make an object move and have an animation attached at the same time? 0 Answers
Make so the script only moves and makes actions to the gameObject that its attached to. 2 Answers