Hello,
Ive been trying for a little bit to implement manual transmission with gear ratios and a clutch into my current code.It is for a big rig and has 2 scripts.1 connects to the car,and the other connects to each wheel.The scripts are below.If you have any advice to implement manual transmission into the script please help if you like.There are not a lot of scripts around about manual transmission and the ones I’ve found i cant get to implement into my script properly.
Car Script
var wheelFL : Transform;
var wheelFR : Transform;
var wheelML : Transform;
var wheelMR : Transform;
var wheelBL : Transform;
var wheelBR : Transform;
var frontWheelSteering = false;
var middleWheelSteering = false;
var rearWheelSteering = false;
var frontWheelDrive = false;
var middleWheelDrive = false;
var rearWheelDrive = false;
var minSteeringAngle = 0.00;
var maxSteeringAngle = 0.00;
var highSpeed = 0.00;
var motorMass = 1.00;
var motorDrag = 0.00;
var maxAcceleration = 0.00;
var maxMotorSpeed = 0.00;
var minTorque = 0.00;
var maxTorque = 0.00;
var centerOfMass : Vector3;
rigidbody.centerOfMass = centerOfMass;
private var scriptWheelFL : Wheel;
private var scriptWheelFR : Wheel;
private var scriptWheelML : Wheel;
private var scriptWheelMR : Wheel;
private var scriptWheelBL : Wheel;
private var scriptWheelBR : Wheel;
scriptWheelFL = wheelFL.GetComponent(Wheel);
scriptWheelFR = wheelFR.GetComponent(Wheel);
scriptWheelML = wheelML.GetComponent(Wheel);
scriptWheelMR = wheelMR.GetComponent(Wheel);
scriptWheelBL = wheelBL.GetComponent(Wheel);
scriptWheelBR = wheelBR.GetComponent(Wheel);
scriptWheelFL.car = this;
scriptWheelFR.car = this;
scriptWheelML.car = this;
scriptWheelMR.car = this;
scriptWheelBL.car = this;
scriptWheelBR.car = this;
var steeringInput = 0.00;
var motorInput = 0.00;
var motorTorque = 0.00;
var motorAccel = 0.00;
var motorSpeed = 0.00;
var steeringAngle = 0.00;
if(frontWheelDrive) scriptWheelFL.driven = scriptWheelFR.driven = true;
if(middleWheelDrive) scriptWheelML.driven = scriptWheelMR.driven = true;
if(rearWheelDrive) scriptWheelBL.driven = scriptWheelBR.driven = true;
function FixedUpdate ()
{
steeringInput = Input.GetAxis("Horizontal");
motorInput = Input.GetAxis("Vertical");
if(frontWheelSteering) wheelFL.localRotation = wheelFR.localRotation = Quaternion.LookRotation(Vector3(steeringInput * (steeringAngle / 90), 0, 1 + (-1 * Mathf.Abs(steeringInput * (steeringAngle / 90))) ));
if(middleWheelSteering) wheelML.localRotation = wheelMR.localRotation = Quaternion.LookRotation(Vector3(-steeringInput * (steeringAngle / 90), 0, 1 + (-1 * Mathf.Abs(steeringInput * (steeringAngle / 90))) ));
if(rearWheelSteering) wheelBL.localRotation = wheelBR.localRotation = Quaternion.LookRotation(Vector3(-steeringInput * (steeringAngle / 90), 0, 1 + (-1 * Mathf.Abs(steeringInput * (steeringAngle / 90))) ));
if(frontWheelDrive || middleWheelDrive || rearWheelDrive)
{
motorTorque = Mathf.Lerp(maxTorque / 30, minTorque / 30, motorSpeed / maxMotorSpeed) * Mathf.Abs(motorInput);
if(motorTorque < 1) motorTorque = 1;
motorAccel = Mathf.Lerp(maxAcceleration, 0, motorSpeed / maxMotorSpeed);
steeringAngle = Mathf.Lerp(maxSteeringAngle, minSteeringAngle, rigidbody.velocity.magnitude / highSpeed);
motorSpeed += motorInput * motorAccel / motorMass * Time.fixedDeltaTime;
if(frontWheelDrive)
{
scriptWheelFL.speed = scriptWheelFR.speed = motorSpeed;
}
if(middleWheelDrive)
{
scriptWheelML.speed = scriptWheelMR.speed = motorSpeed;
}
if(rearWheelDrive)
{
scriptWheelBL.speed = scriptWheelBR.speed = motorSpeed;
}
motorSpeed += -motorSpeed * motorDrag / motorTorque * Time.fixedDeltaTime;
}
}
function AddForceOnMotor (force : float)
{
motorSpeed += force / motorTorque;
}
Wheel Script
var mass = 1.00;
var wheelRadius = 0.00;
var suspensionRange = 0.00;
var suspensionForce = 0.00;
var suspensionDamp = 0.00;
var compressionFrictionFactor = 0.00;
var sidewaysFriction = 0.00;
var sidewaysDamp = 0.00;
var sidewaysSlipVelocity = 0.00;
var sidewaysSlipForce = 0.00;
var sidewaysSlipFriction = 0.00;
var sidewaysStiffnessFactor = 0.00;
var forwardFriction = 0.00;
var forwardSlipVelocity = 0.00;
var forwardSlipForce = 0.00;
var forwardSlipFriction = 0.00;
var forwardStiffnessFactor = 0.00;
var frictionSmoothing = 1.00;
private var hit : RaycastHit;
private var parent : Rigidbody;
parent = transform.root.rigidbody;
private var graphic : Transform;
graphic = transform.FindChild("Graphic");
private var wheelCircumference = 0.00;
wheelCircumference = wheelRadius * Mathf.PI * 2;
private var usedSideFriction = 0.00;
private var usedForwardFriction = 0.00;
private var sideSlip = 0.00;
private var forwardSlip = 0.00;
var car : Car;
var driven = false;
var speed = 0.00;
var brake = false;
var skidbrake = false;
function FixedUpdate ()
{
down = transform.TransformDirection(Vector3.down);
if(Physics.Raycast (transform.position, down, hit, suspensionRange + wheelRadius) hit.collider.transform.root != transform.root)
{
velocityAtTouch = parent.GetPointVelocity(hit.point);
compression = hit.distance / (suspensionRange + wheelRadius);
compression = -compression + 1;
force = -down * compression * suspensionForce;
t = transform.InverseTransformDirection(velocityAtTouch);
t.z = t.x = 0;
shockDrag = transform.TransformDirection(t) * -suspensionDamp;
forwardDifference = transform.InverseTransformDirection(velocityAtTouch).z - speed;
newForwardFriction = Mathf.Lerp(forwardFriction, forwardSlipFriction, forwardSlip * forwardSlip);
newForwardFriction = Mathf.Lerp(newForwardFriction, newForwardFriction * compression * 1, compressionFrictionFactor);
if(frictionSmoothing > 0)
usedForwardFriction = Mathf.Lerp(usedForwardFriction, newForwardFriction, Time.fixedDeltaTime / frictionSmoothing);
else
usedForwardFriction = newForwardFriction;
forwardForce = transform.TransformDirection(Vector3(0, 0, -forwardDifference)) * usedForwardFriction;
forwardSlip = Mathf.Lerp(forwardForce.magnitude / forwardSlipForce, forwardDifference / forwardSlipVelocity, forwardStiffnessFactor);
sidewaysDifference = transform.InverseTransformDirection(velocityAtTouch).x;
newSideFriction = Mathf.Lerp(sidewaysFriction, sidewaysSlipFriction, sideSlip * sideSlip);
newSideFriction = Mathf.Lerp(newSideFriction, newSideFriction * compression * 1, compressionFrictionFactor);
if(frictionSmoothing > 0)
usedSideFriction = Mathf.Lerp(usedSideFriction, newSideFriction, Time.fixedDeltaTime / frictionSmoothing);
else
usedSideFriction = newSideFriction;
sideForce = transform.TransformDirection(Vector3(-sidewaysDifference, 0, 0)) * usedSideFriction;
sideSlip = Mathf.Lerp(sideForce.magnitude / sidewaysSlipForce, sidewaysDifference / sidewaysSlipVelocity, sidewaysStiffnessFactor);
t = transform.InverseTransformDirection(velocityAtTouch);
t.z = t.y = 0;
sideDrag = transform.TransformDirection(t) * -sidewaysDamp;
parent.AddForceAtPosition(force + shockDrag + forwardForce + sideForce + sideDrag, hit.point);
if(driven)
car.AddForceOnMotor (forwardDifference * usedForwardFriction * Time.fixedDeltaTime);
else
speed += forwardDifference;
graphic.position = transform.position + (down * (hit.distance - wheelRadius));
}
else
{
graphic.position = transform.position + (down * suspensionRange);
}
graphic.transform.Rotate(360 * (speed / wheelCircumference) * Time.fixedDeltaTime, 0, 0);
}