My tires spin perfectly the way I want them too, however I can seem to find a straightforward tutorial or free piece of code (in C#) that can help me Turn the tires in the same angle as my wheel colliders.
I would appreciate if someone could point me in the right direction! I’ve been searching for about 3 hours and all I can seem to find are people asking how to rotate the tires which is something I have already figured out.
So your tire objects control their rotation on the Z and you want to set their rotation on the Y to match the wheel collider so they turn left and right?
Should just be:
Quaternion tireRotation = tire.transform.rotation;
Quaternion colliderRotation = collider.transform.rotation;
tire.transform.rotation = Quaternion.Euler(tireRotation.eulerAngles.x, colliderRotation.eulerAngles.y, tireRotation.eulerAngles.z);
My code is as follows but I got the following error:
Assets/Scripts/HarvMovementScript.cs(17,35): error CS0236: A field initializer cannot reference the nonstatic field, method, or property HarvMovementScript.WheelsFL' & Assets/Scripts/HarvMovementScript.cs(18,39): error CS0236: A field initializer cannot reference the nonstatic field, method, or property
HarvMovementScript.WheelColliderFL’
using UnityEngine;
using System.Collections;
public class HarvMovementScript : MonoBehaviour {
public float MotorForce;
public WheelCollider WheelColliderFL;
public WheelCollider WheelColliderFR;
public WheelCollider WheelColliderRL;
public WheelCollider WheelColliderRR;
public Transform WheelsFL; // front left wheel mesh only ( no collider)
public Transform WheelsFR; // front right wheel mesh only ( no collider)
public Transform WheelsBL; // back left wheel mesh only ( no collider)
public Transform WheelsBR; // back right wheel mesh only ( no collider)
public float SteerForce;
public float BrakeForce;
Quaternion tireRotation = WheelsFL.transform.rotation;
Quaternion colliderRotation = WheelColliderFL.transform.rotation;
///public float TurnSpeed;
///public float TurnAngle;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float v = Input.GetAxis ("Vertical") * MotorForce;
float h = Input.GetAxis ("Horizontal") * SteerForce;
WheelColliderRL.motorTorque = v;
WheelColliderFL.motorTorque = v;
WheelColliderRR.motorTorque = v;
WheelColliderFR.motorTorque = v;
WheelColliderFL.steerAngle = h;
WheelColliderFR.steerAngle = h;
WheelColliderFL.transform.rotation = Quaternion.Euler(tireRotation.eulerAngles.x, colliderRotation.eulerAngles.y, tireRotation.eulerAngles.z);
WheelsFL.Rotate(WheelColliderFL.rpm / 60 * 360 * Time.deltaTime,0,0);
WheelsFR.Rotate(WheelColliderFR.rpm / 60 * 360 * Time.deltaTime,0,0);
WheelsBL.Rotate(WheelColliderFL.rpm / 60 * 360 * Time.deltaTime,0,0);
WheelsBR.Rotate(WheelColliderFR.rpm / 60 * 360 * Time.deltaTime,0,0);
///Brakeon
if (Input.GetKey (KeyCode.Space)) {
WheelColliderRL.brakeTorque = BrakeForce;
WheelColliderRR.brakeTorque = BrakeForce;
WheelColliderFL.brakeTorque = BrakeForce;
WheelColliderFR.brakeTorque = BrakeForce;
}
///Brakeoff
if (Input.GetKeyUp (KeyCode.Space)) {
WheelColliderRL.brakeTorque = 0;
WheelColliderRR.brakeTorque = 0;
WheelColliderFL.brakeTorque = 0;
WheelColliderFR.brakeTorque = 0;
}
}
}
Move these two lines:
Quaternion tireRotation = WheelsFL.transform.rotation;
Quaternion colliderRotation = WheelColliderFL.transform.rotation;
Down to just before this line:
WheelColliderFL.transform.rotation = Quaternion.Euler(tireRotation.eulerAngles.x, colliderRotation.eulerAngles.y, tireRotation.eulerAngles.z);
Quaternions are structs (value types), so you can’t store references to them like you can classes (reference types).