Search Unity

Driving Through Wheel Colliders; Transform Errors

Discussion in 'Editor & General Support' started by jacob_p01, Jan 15, 2019.

  1. jacob_p01

    jacob_p01

    Joined:
    Nov 20, 2018
    Posts:
    8
    I've been working on creating a basic player-controller for a vehicle as I'm getting into unity3D. I'm having an error with my wheel transforms, while the colliders work as expected and the truck drives as normal, the transforms seem to be rotating away from the truck around their pivots, instead of on their axis.
    Seen here:


    The script is as follows:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SimpleCarController : MonoBehaviour {
    6.  
    7.     private float m_horizontalInput;
    8.     private float m_verticalInput;
    9.     private float m_steeringAngle;
    10.  
    11.     public WheelCollider frontDriverW, frontPassngerW;
    12.     public WheelCollider rearDriverW, rearPassengerW;
    13.     public Transform frontDriverT, frontPassengerT;
    14.     public Transform rearDriverT, rearPassengerT;
    15.     public float maxSteerAngle = 30;
    16.     public float motorForce = 50;
    17.  
    18.     public void GetInput()
    19.     {
    20.         m_horizontalInput = Input.GetAxis("Horizontal");
    21.         m_verticalInput = Input.GetAxis("Vertical");
    22.     }
    23.    
    24.     private void Steer()
    25.     {
    26.         m_steeringAngle = maxSteerAngle * m_horizontalInput;
    27.         frontDriverW.steerAngle = m_steeringAngle;
    28.         frontPassngerW.steerAngle = m_steeringAngle;
    29.  
    30.  
    31.     }
    32.  
    33.     private void Accelerate()
    34.     {
    35.         frontDriverW.motorTorque = m_verticalInput * motorForce;
    36.         frontPassngerW.motorTorque = m_verticalInput * motorForce;
    37.     }
    38.  
    39.     private void UpdateWheelPoses()
    40.     {
    41.  
    42.         UpdateWheelPose(frontDriverW, frontDriverT);
    43.         UpdateWheelPose(frontPassngerW, frontPassengerT);
    44.         UpdateWheelPose(rearDriverW, rearDriverT);
    45.         UpdateWheelPose(rearPassengerW, rearPassengerT);
    46.     }
    47.  
    48.     private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
    49.     {
    50.         Vector3 _pos = _transform.position;
    51.         Quaternion _quat = _transform.rotation;
    52.  
    53.         _collider.GetWorldPose(out _pos, out _quat);
    54.  
    55.         _transform.position = _pos;
    56.         _transform.rotation = _quat;
    57.     }
    58.  
    59.     private void FixedUpdate()
    60.     {
    61.         GetInput();
    62.         Steer();
    63.         Accelerate();
    64.         UpdateWheelPoses();
    65.     }
    66.  
    67.    
    68.  
    69. }