Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity wheelcollider tutorial

Discussion in 'Scripting' started by nutbolt, Jan 9, 2016.

  1. nutbolt

    nutbolt

    Joined:
    Jan 9, 2016
    Posts:
    2
    I have been following the Unity tutorial on wheelcolliders here:
    http://docs.unity3d.com/Manual/WheelColliderTutorial.html

    The last script on the page, when I paste it into monodevelop it comes up with errors on line 14

    Code (CSharp):
    1. public class SimpleCarController : MonoBehaviour {
    2.     public List(AxleInfo) axleInfos;
    3.     public float maxMotorTorque;
    4.     public float maxSteeringAngle;

    Line 14 method must have a return type when I debug it

    Any ideas?

    The complete script here if you want to try yourself:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [System.Serializable]
    6. public class AxleInfo {
    7.     public WheelCollider leftWheel;
    8.     public WheelCollider rightWheel;
    9.     public bool motor;
    10.     public bool steering;
    11. }
    12.  
    13. public class SimpleCarController : MonoBehaviour {
    14.     public List(AxleInfo) axleInfos;
    15.     public float maxMotorTorque;
    16.     public float maxSteeringAngle;
    17.  
    18.     // finds the corresponding visual wheel
    19.     // correctly applies the transform
    20.     public void ApplyLocalPositionToVisuals(WheelCollider collider)
    21.     {
    22.         if (collider.transform.childCount == 0) {
    23.             return;
    24.         }
    25.  
    26.         Transform visualWheel = collider.transform.GetChild(0);
    27.  
    28.         Vector3 position;
    29.         Quaternion rotation;
    30.         collider.GetWorldPose(out position, out rotation);
    31.  
    32.         visualWheel.transform.position = position;
    33.         visualWheel.transform.rotation = rotation;
    34.     }
    35.  
    36.     public void FixedUpdate()
    37.     {
    38.         float motor = maxMotorTorque * Input.GetAxis("Vertical");
    39.         float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
    40.  
    41.         foreach (AxleInfo axleInfo in axleInfos) {
    42.             if (axleInfo.steering) {
    43.                 axleInfo.leftWheel.steerAngle = steering;
    44.                 axleInfo.rightWheel.steerAngle = steering;
    45.             }
    46.             if (axleInfo.motor) {
    47.                 axleInfo.leftWheel.motorTorque = motor;
    48.                 axleInfo.rightWheel.motorTorque = motor;
    49.             }
    50.             ApplyLocalPositionToVisuals(axleInfo.leftWheel);
    51.             ApplyLocalPositionToVisuals(axleInfo.rightWheel);
    52.         }
    53.     }
    54. }
     
    Last edited: Jan 9, 2016
  2. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    Digging...
     
  3. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
  4. nutbolt

    nutbolt

    Joined:
    Jan 9, 2016
    Posts:
    2
    It works but the wheels are not in the way intended. Who actually writes the scripts as they are not fit for purpose as tutorials. My tutorial car now has front flippers, not wheels
     
  5. Dario_Zuban

    Dario_Zuban

    Joined:
    Oct 24, 2017
    Posts:
    2
    Same problem here, without visual tires it works just fine but once I add cylinders, the script resets my rotaion and I get front flippers instead of wheels.
    A workaround would be to add the tires seperatly, then again, they don't turn and/or spin
     
  6. Dario_Zuban

    Dario_Zuban

    Joined:
    Oct 24, 2017
    Posts:
    2
    I actually just found the sloution:
    https://unity3d.com/profiles/unity3d/themes/unity/resources/downloads/beta/unity-5.0-user-guide.pdf

    this is the same tutorial, but not broken. The trick is to insert an empty GameObject into the WheelCollider and inside the empty Object a cylinder...