Search Unity

Any tutorials on Tires turning with Wheel colliders (4.6)?

Discussion in 'Scripting' started by Zeldafreakneo, Mar 28, 2015.

  1. Zeldafreakneo

    Zeldafreakneo

    Joined:
    Mar 21, 2015
    Posts:
    9
    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.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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:
    Code (csharp):
    1.  
    2. Quaternion tireRotation = tire.transform.rotation;
    3. Quaternion colliderRotation = collider.transform.rotation;
    4.  
    5. tire.transform.rotation = Quaternion.Euler(tireRotation.eulerAngles.x, colliderRotation.eulerAngles.y, tireRotation.eulerAngles.z);
    6.  
     
  3. Zeldafreakneo

    Zeldafreakneo

    Joined:
    Mar 21, 2015
    Posts:
    9
    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'



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HarvMovementScript : MonoBehaviour {
    5.  
    6.     public float MotorForce;
    7.     public WheelCollider WheelColliderFL;
    8.     public WheelCollider WheelColliderFR;
    9.     public WheelCollider WheelColliderRL;
    10.     public WheelCollider WheelColliderRR;
    11.     public Transform WheelsFL; // front left wheel mesh only ( no collider)
    12.     public Transform WheelsFR; // front right wheel mesh only ( no collider)
    13.     public Transform WheelsBL; // back left wheel mesh only ( no collider)
    14.     public Transform WheelsBR; // back right wheel mesh only ( no collider)
    15.     public float SteerForce;
    16.     public float BrakeForce;
    17.     Quaternion tireRotation = WheelsFL.transform.rotation;
    18.     Quaternion colliderRotation = WheelColliderFL.transform.rotation;
    19.  
    20.     ///public float TurnSpeed;
    21.     ///public float TurnAngle;
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.    
    26.  
    27.  
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.    
    33.         float v = Input.GetAxis ("Vertical") * MotorForce;
    34.         float h = Input.GetAxis ("Horizontal") * SteerForce;
    35.        
    36.  
    37.         WheelColliderRL.motorTorque = v;
    38.         WheelColliderFL.motorTorque = v;
    39.         WheelColliderRR.motorTorque = v;
    40.         WheelColliderFR.motorTorque = v;
    41.  
    42.         WheelColliderFL.steerAngle = h;
    43.         WheelColliderFR.steerAngle = h;
    44.  
    45.         WheelColliderFL.transform.rotation = Quaternion.Euler(tireRotation.eulerAngles.x, colliderRotation.eulerAngles.y, tireRotation.eulerAngles.z);
    46.  
    47.        
    48.  
    49.  
    50.         WheelsFL.Rotate(WheelColliderFL.rpm / 60 * 360 * Time.deltaTime,0,0);
    51.         WheelsFR.Rotate(WheelColliderFR.rpm / 60 * 360 * Time.deltaTime,0,0);
    52.         WheelsBL.Rotate(WheelColliderFL.rpm / 60 * 360 * Time.deltaTime,0,0);
    53.         WheelsBR.Rotate(WheelColliderFR.rpm / 60 * 360 * Time.deltaTime,0,0);
    54.  
    55.  
    56.  
    57.         ///Brakeon
    58.         if (Input.GetKey (KeyCode.Space)) {
    59.        
    60.         WheelColliderRL.brakeTorque = BrakeForce;
    61.         WheelColliderRR.brakeTorque = BrakeForce;
    62.         WheelColliderFL.brakeTorque = BrakeForce;
    63.         WheelColliderFR.brakeTorque = BrakeForce;
    64.         }
    65.  
    66.         ///Brakeoff
    67.         if (Input.GetKeyUp (KeyCode.Space)) {
    68.                         WheelColliderRL.brakeTorque = 0;
    69.                         WheelColliderRR.brakeTorque = 0;
    70.                         WheelColliderFL.brakeTorque = 0;
    71.                         WheelColliderFR.brakeTorque = 0;
    72.  
    73.                 }
    74.            
    75.  
    76.  
    77.     }
    78. }
    79.  
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Move these two lines:
    Code (csharp):
    1.  
    2.   Quaternion tireRotation = WheelsFL.transform.rotation;
    3.   Quaternion colliderRotation = WheelColliderFL.transform.rotation;
    4.  
    Down to just before this line:
    Code (csharp):
    1.  
    2. WheelColliderFL.transform.rotation = Quaternion.Euler(tireRotation.eulerAngles.x, colliderRotation.eulerAngles.y, tireRotation.eulerAngles.z);
    3.  
    Quaternions are structs (value types), so you can't store references to them like you can classes (reference types).