- Home /
This post has been wikified, any user with enough reputation can edit it.
Return to initial position and rotation
The project refers to a steering and suspension system from a car. The problem is that when I am releasing the arrows ( left <- or right ->) from the keyboard the wheel colliders are returning to their initial position. I want to put some code into the script file so the whole system will return to its initial position no turn button is pressed. Any idea please; Here is my code:
Script that turning left and right the wheels:
#pragma strict
function FixedUpdate () {
if (Input.GetKey("right") && !Input.GetKey("left") && turning.right == true )
{
transform.Rotate(0,1,0);
turning.left = true;
}
else if (Input.GetKey("left") && !Input.GetKey("right") && turning.left == true)
{
transform.Rotate(0,-1,0);
turning.right = true;
}
}
Script that controls the car:
#pragma strict
var FL : WheelCollider;
var FR : WheelCollider;
var RL : WheelCollider;
var RR : WheelCollider;
var WheelFL: Transform;
var WheelFR: Transform;
var maxTorque : float = 10;
var decellaration: float = 10;
public static var steeringAngle: float = 0;
function Start () {
rigidbody.centerOfMass.y = - 10;
}
function FixedUpdate () {
RR.motorTorque = maxTorque * Input.GetAxis("Vertical");
RL.motorTorque = maxTorque * Input.GetAxis("Vertical") ;
FL.steerAngle = 19 * Input.GetAxis("Horizontal");
FR.steerAngle = 19 * Input.GetAxis("Horizontal");
if ( Input.GetButton("Vertical") == false){
RR.brakeTorque = decellaration;
RL.brakeTorque = decellaration;
}
else{
RR.brakeTorque = 0;
RL.brakeTorque = 0;
}
}
function Update(){
WheelFL.Rotate(0,0,- FL.rpm / 60 * 360 * Time.deltaTime);
WheelFR.Rotate(0,0,- FR.rpm / 60 * 360 * Time.deltaTime);
CarControlerScript.steeringAngle = FL.steerAngle;
}
Is there a way to connect those two scripts so when the wheel colliders return to their initial position the wheels of the car return too?
colliders.png
(480.2 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220611232005im_/https://answers.unity.com/themes/thub/images/avi.jpg)