I have made a script of a car, but it seems to tremble
Does anyone know why this happens? Is there anything to do with the physics?
Hey there,
Would you mind posting some more information about the script? Without additional details it’s a little hard to find out what exactly is going wrong. The trembling effect could be any number of things. You always have to be careful when working with rigidbodies, not to have forces that fight each other. Is it possible that some of your movement calls are being fired simultaneously? Sometimes it helps to do a line by line inspection of the code, commenting out certain functions to find out exactly where the code is messing up.
Of course it could also be an issue with colliders and environmental mishaps, which is another reason to maybe post a portion of your code so we can see what’s going on.
-McMayhem
Mind you, the car trembled with very little coding, but I went further with coding even though the car trembled or collided with the air after a certain speed.
Also I don’t understand the wheelcollider options, like the springs and stuff like that
Here ya go!
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour {
public WheelCollider wheelFL;
public WheelCollider wheelFR;
public WheelCollider wheelRL;
public WheelCollider wheelRR;
public Transform wheelFLTrans;
public Transform wheelFRTrans;
public Transform wheelRLTrans;
public Transform wheelRRTrans;
public float lowSpeed = 50;
public float lowSpeedSteerAngle = 10;
public float highSpeedSteerAngle = 1;
public float decceleration = 30;
public float maxTorque = 5.0f;
public float currentSpeed;
public float topSpeed = 150;
public float maxReverseSpeed = 50;
public GameObject backLightObject;
public Material idleLightMaterial;
public Material brakeLightMaterial;
public Material reverseLightMaterial;
// Use this for initialization
void Start () {
rigidbody.centerOfMass = new Vector3(0, -0.9f, 0);
}
// Physics update
void FixedUpdate () {
Controller ();
}
// Update is called once per frame
void Update () {
wheelFLTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
wheelFRTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
wheelRLTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
wheelRRTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
Vector3 temp = wheelFLTrans.localEulerAngles;
temp.y = wheelFL.steerAngle - wheelFLTrans.localEulerAngles.z;
wheelFLTrans.localEulerAngles = temp;
temp = wheelFRTrans.localEulerAngles;
temp.y = wheelFR.steerAngle - wheelFRTrans.localEulerAngles.z;
wheelFRTrans.localEulerAngles = temp;
// External functions
BackLight();
}
void Controller(){
// Current speed of the car in km/h
currentSpeed = 2*22/7*wheelRL.radius*wheelRL.rpm*60/1000;
// Round the speed so it will show a rounded number
currentSpeed = Mathf.Round (currentSpeed);
// Stop exceeding top speed
if(currentSpeed < topSpeed currentSpeed > -maxReverseSpeed)
{
// Movement of the car
wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
}
else
{
wheelRR.motorTorque = 0;
wheelRL.motorTorque = 0;
}
// Slow down car when not pressing any acceleration key
if(Input.GetButton("Vertical") == false)
{
wheelRR.brakeTorque = decceleration;
wheelRL.brakeTorque = decceleration;
}
else
{
wheelRR.brakeTorque = 0;
wheelRL.brakeTorque = 0;
}
float speedFactor = rigidbody.velocity.magnitude/lowSpeed;
float currentSteerAngle = Mathf.Lerp (lowSpeedSteerAngle,highSpeedSteerAngle, speedFactor);
currentSteerAngle *= Input.GetAxis ("Horizontal");
wheelFL.steerAngle = currentSteerAngle;
wheelFR.steerAngle = currentSteerAngle;
}
void BackLight()
{
if(currentSpeed > 0 Input.GetAxis ("Vertical") < 0)
{
backLightObject.renderer.material = brakeLightMaterial;
}
else if(currentSpeed < 0 Input.GetAxis("Vertical") > 0)
{
backLightObject.renderer.material = brakeLightMaterial;
}
else if(currentSpeed < 0 Input.GetAxis("Vertical") > 0)
{
backLightObject.renderer.material = reverseLightMaterial;
}
else
{
backLightObject.renderer.material = idleLightMaterial;
}
}