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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Problem with car

Discussion in 'Scripting' started by szpinak9, Apr 1, 2020.

  1. szpinak9

    szpinak9

    Joined:
    Mar 22, 2020
    Posts:
    8
    Hello i followed tutorial about wheel colliders from youtube but when i hit play my wheels are rotated in Z axis

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

    Greetings.
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      122.6 KB
      Views:
      285
    • 2.jpg
      2.jpg
      File size:
      113.3 KB
      Views:
      285
  2. szpinak9

    szpinak9

    Joined:
    Mar 22, 2020
    Posts:
    8
    Ok I figured it out by adding:
    _quat = _quat * Quaternion.Euler(new Vector3(0, 0, 90));

    Just in case somebody needed it.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Excellent. Another approach is to put in anchor GameObjects that your wheels attach to, and then you can orient those anchors the way you want and leave the wheels neutral.