Search Unity

Car Physics

Discussion in 'Physics' started by ultimategamersultimategamers, Jun 7, 2020.

  1. ultimategamersultimategamers

    ultimategamersultimategamers

    Joined:
    Jun 7, 2020
    Posts:
    2
    Hello, i recently started new project, and have some problems with my car physics. I was searching the internet all day and found this script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BMW_i8 : MonoBehaviour
    6. {
    7.     private float m_horizontalInput;  // A & D
    8.     private float m_verticalInput; // W & S
    9.     private float m_steeringAngle; // Modified by m_horizontalInput
    10.  
    11.     public WheelCollider frontDriverW, frontPassengerW; //W = Wheel
    12.     public WheelCollider rearDriverW, rearPassengerW;
    13.     public Transform frontDriverT, frontPassengerT; //T = Transform
    14.     public Transform rearDriverT, rearPassengerT;
    15.  
    16.     public float maxSteerAngle = 30; // 30 Degrees Max Steering Angle
    17.     public float motorForce = 50; // Motor Torque Variable
    18.  
    19.     ////////////////////////////////////////////////////////////////////////////////////////////////////
    20.  
    21.     public void GetInput()
    22.     {
    23.         m_horizontalInput = Input.GetAxis("Horizontal"); //Unity Input Method (Edit > Project Settings > Input)
    24.         m_verticalInput = Input.GetAxis("Vertical");
    25.     }
    26.     private void Steer()
    27.     {
    28.         m_steeringAngle = maxSteerAngle * m_horizontalInput;
    29.         frontDriverW.steerAngle = m_steeringAngle;
    30.         frontPassengerW.steerAngle = m_steeringAngle;
    31.     }
    32.     private void Accelerate()
    33.     {
    34.         frontDriverW.motorTorque = m_verticalInput * motorForce;
    35.         frontPassengerW.motorTorque = m_verticalInput * motorForce;
    36.  
    37.     }
    38.     private void UpdateWheelPoses()
    39.     {
    40.         UpdateWheelPose(frontDriverW, frontDriverT); // Taking Pose Information from _collider and Applying it to the _transform
    41.         UpdateWheelPose(frontPassengerW, frontPassengerT);
    42.         UpdateWheelPose(rearDriverW, rearDriverT);
    43.         UpdateWheelPose(rearPassengerW, rearPassengerT);
    44.     }
    45.     private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
    46.     {
    47.         Vector3 _pos = _transform.position;
    48.         Quaternion _quat = _transform.rotation;
    49.  
    50.         _collider.GetWorldPose(out _pos, out _quat); // Out Values
    51.  
    52.         _transform.position = _pos;
    53.         _transform.rotation = _quat;
    54.     }
    55.     private void FixedUpdate()
    56.     {
    57.         GetInput();
    58.         Steer();
    59.         Accelerate();
    60.         UpdateWheelPoses();
    61.     }
    62. }
    63.  
    which i use in my game. Here's video from my problem:

    I have some stutters while driving, and most important bug is that whatever float i set speed to be, it is approximately always the same, can't go faster. Any tips?
     
  2. Erakk

    Erakk

    Joined:
    Mar 26, 2013
    Posts:
    1
    TL;DR: Increase your motorForce value to more than 50.

    You are moving a Car that weights 1500 kilograms with a force of 100 newtons (50 on each rear wheel).
    Thats an acceleration of (a = F/m) 0.066 m/s^2, which for what you want is probably very low.
    Edit: Disregard, as @AlTheSlacker pointed out, I confused Torque and Force...
     
    Last edited: Jun 9, 2020
  3. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    I see you tried different torque values and it made no difference (incidentally, @Erakk don't confuse torque and force). Have you looked to see how much slip you are getting from the driven wheels? You can get this from GetGroundHit -> WheelHit. I wonder if you are just spinning the wheels.

    Have you tried to print() m_verticalInput * motorForce from the Accelerate() method to confirm you are reading the controller input correctly? I think the key press routines should be checked in the Update() method not the FixedUpdate()

    I see you have wheel damping rate set to 1, which I find can be confusing, especially when trying to debug, although I don't think it will be your problem, you could try setting it to 0.0001 (minimum value).
     
  4. ultimategamersultimategamers

    ultimategamersultimategamers

    Joined:
    Jun 7, 2020
    Posts:
    2
    I said, no matter which value i set it to, it is always the same