- Home /
Very Simple Car Questions
So in the game I'm making, you essentially have to drive a little truck to the pickup station, get the item that needs the delivery, then take it to the destination. I'm working on just the first level which is a straight run with no obstacles so I'm purely working on the car physics right now. I've got a simple car going, but it flips so incredibly easy it's pathetic and the car is hardly moving at all. The car is never supposed to catch any air so is there any way to constrain it to the ground? It's going to be top down so the Y-axis isn't even a factor in the game.
I lightened it up so it's more visible what's actually going on. Here's the Mac and PC stand alones, not sure how to put it as a playable game online...
You answered yourself... "constraints" ... On your rigidbody options, contraints, freeze the Y position so it cant ever move up or down.
Answer by Lo0NuhtiK · Jan 01, 2012 at 03:59 AM
I haven't made a car in unity yet, but from what I read in the docs you should use Brake Torque for braking. Something like this maybe->
var brakeButton : String = "MyInputBrakeButtonName" ;
var brakePower : float = 100.0 ;
//in update() with your other stuff->
if(Input.GetButton(brakeButton)){
FLWheel.brakeTorque = brakePower ;
FRWheel.brakeTorque = brakePower ;
}
Note: you're only going to have front brakes like that lol
Answer by kewlhead · Dec 31, 2011 at 11:27 AM
That never even crossed my mind... Thanks:) Will that solve the rolling do you think?
You can play around with the constraint freezers and see if you can get it set up how you want it. It can freeze positions and/or rotations.
Answer by kewlhead · Jan 01, 2012 at 12:31 AM
One last question, now that I've got that all sorted out, thank you:), I'm not sure how to make a brake system. Simply pressing reverse takes forever to slow it down. I figure I have to alter the velocity in some way but have no idea how...
Here's my code:
var FLWheel : WheelCollider;
var FRWheel : WheelCollider;
var maxForwardSpeed : float= 50.0;
var maxReverseSpeed : float= 50.0;
var turnRadius : float= 30.0;
var EngineTorque : float = 25;
function Start () {
rigidbody.centerOfMass += Vector3(0, -.5, 0);
}
function Update () {
/*Apply forward motion (Torque) to the front wheels*/
FLWheel.motorTorque = -EngineTorque * 10 * Input.GetAxis("Vertical");
FRWheel.motorTorque = -EngineTorque * 10 * Input.GetAxis("Vertical");
/*This limits the car to a max speed. It is negative because the input for up and down
are reversed for whatever reason*/
if(FLWheel.motorTorque && FRWheel.motorTorque < maxForwardSpeed){
FLWheel.motorTorque = -maxForwardSpeed;
FRWheel.motorTorque = -maxForwardSpeed;
}
/*This limits the car to a max speed when travelling in reverse. Horizontal input is correct,
so no negatives need to be put in place.*/
if(FLWheel.motorTorque && FRWheel.motorTorque > maxReverseSpeed){
FLWheel.motorTorque = maxReverseSpeed;
FRWheel.motorTorque = maxReverseSpeed;
}
/*Turn Radius variable increases or decreases the tightness of the turn*/
FLWheel.steerAngle = turnRadius * Input.GetAxis("Horizontal");
FRWheel.steerAngle = turnRadius * Input.GetAxis("Horizontal");
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX;
rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with vehicle script 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Vehicle Script Debugging 1 Answer
Vehicle/car/ Movement 0 Answers