Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Car Acceleration is mindblowingly slow.

Discussion in 'Scripting' started by RubenVanOostveen, Jan 13, 2021.

  1. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    I just need it to get up to speed faster. I tried:
    1. Weight
    2. Wheel mass
    3. re Size model
    4. did stiffen suspension with Wheel Colliders
    5. modify the drag

    This all doesn't work for me. the car is up to max speed in about 2 minutes when it should be 30 seconds in real life.
    PHOTOS:
    The car itself:
    upload_2021-1-13_13-45-23.png
    This is one wheel collider (All same settings):
    upload_2021-1-13_13-46-59.png
    This is my code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CarController : MonoBehaviour
    7. {
    8.     private float horizontalInput;
    9.     private float verticalInput;
    10.     private float steerAngle;
    11.     private bool isBreaking;
    12.  
    13.     public WheelCollider frontLeftWheelCollider;
    14.     public WheelCollider frontRightWheelCollider;
    15.     public WheelCollider rearLeftWheelCollider;
    16.     public WheelCollider rearRightWheelCollider;
    17.     public Transform frontLeftWheelTransform;
    18.     public Transform frontRightWheelTransform;
    19.     public Transform rearLeftWheelTransform;
    20.     public Transform rearRightWheelTransform;
    21.  
    22.     public float maxSteeringAngle = 30f;
    23.     public float motorForce = 50f;
    24.     public float brakeForce = 0f;
    25.  
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         GetInput();
    30.         HandleMotor();
    31.         HandleSteering();
    32.         UpdateWheels();
    33.     }
    34.  
    35.     private void GetInput()
    36.     {
    37.         horizontalInput = Input.GetAxis("Horizontal");
    38.         verticalInput = Input.GetAxis("Vertical");
    39.         isBreaking = Input.GetKey(KeyCode.Space);
    40.     }
    41.  
    42.     private void HandleSteering()
    43.     {
    44.         steerAngle = maxSteeringAngle * horizontalInput;
    45.         frontLeftWheelCollider.steerAngle = steerAngle;
    46.         frontRightWheelCollider.steerAngle = steerAngle;
    47.     }
    48.  
    49.     private void HandleMotor()
    50.     {
    51.         frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
    52.         frontRightWheelCollider.motorTorque = verticalInput * motorForce;
    53.  
    54.         brakeForce = isBreaking ? 3000f : 0f;
    55.         frontLeftWheelCollider.brakeTorque = brakeForce;
    56.         frontRightWheelCollider.brakeTorque = brakeForce;
    57.         rearLeftWheelCollider.brakeTorque = brakeForce;
    58.         rearRightWheelCollider.brakeTorque = brakeForce;
    59.     }
    60.  
    61.     private void UpdateWheels()
    62.     {
    63.         UpdateWheelPos(frontLeftWheelCollider, frontLeftWheelTransform);
    64.         UpdateWheelPos(frontRightWheelCollider, frontRightWheelTransform);
    65.         UpdateWheelPos(rearLeftWheelCollider, rearLeftWheelTransform);
    66.         UpdateWheelPos(rearRightWheelCollider, rearRightWheelTransform);
    67.     }
    68.  
    69.     private void UpdateWheelPos(WheelCollider wheelCollider, Transform trans)
    70.     {
    71.         Vector3 pos;
    72.         Quaternion rot;
    73.         wheelCollider.GetWorldPose(out pos, out rot);
    74.         trans.rotation = rot;
    75.         trans.position = pos;
    76.     }
    77.  
    78. }
    How can I make it accelerate faster?

    (Edit: if I need to modify my script please say which line with what I'm normally modeling content not coding I'm new to C3 thank you in advanced!)
     
  2. Crimsonking667

    Crimsonking667

    Joined:
    Dec 17, 2019
    Posts:
    3
    Hi!

    I don't know if it can help you, but what I did a long time ago is add more power to the engine to be able to accelerate as fast as possible.

    Try this:

    Code (CSharp):
    1.  
    2.  
    3. frontLeftWheelCollider.motorTorque = verticalInput * 15 * motorForce;
    4. frontRightWheelCollider.motorTorque = verticalInput * 15 * motorForce;
    5.  
    6.  
     
  3. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91

    Code (CSharp):
    1.   private void HandleMotor()
    2.     {
    3.         frontLeftWheelCollider.motorTorque = verticalInput * 1000 * motorForce;
    4.         frontRightWheelCollider.motorTorque = verticalInput * 1000 * motorForce;
    5.  
    6.         brakeForce = isBreaking ? 3000f : 0f;
    7.         frontLeftWheelCollider.brakeTorque = brakeForce;
    8.         frontRightWheelCollider.brakeTorque = brakeForce;
    9.         rearLeftWheelCollider.brakeTorque = brakeForce;
    10.         rearRightWheelCollider.brakeTorque = brakeForce;
    11.     }
    this still doesn't work it still is waaay too slow
     
  4. Crimsonking667

    Crimsonking667

    Joined:
    Dec 17, 2019
    Posts:
    3
    May be yo can modify motorForce. Or mass to rigidbody.

    If you want, tomorrow shares with you my code. Maybe help you
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    If you are really trying to simulate wheels spinning to accelerate the car, be aware that you will soon run into all kinds of physics stability limitations as the wheels spin faster and faster.

    To this end the default limit to Unity's angular rotation on a Rigidbody is only about one revolution per second (7 rad / second), no matter how much torque you apply to it. That's to keep things stable.

    You can tweak this limit on your wheels:

    https://docs.unity3d.com/ScriptReference/Rigidbody-maxAngularVelocity.html

    I quote from the above documentation:

    The maximimum angular velocity of the rigidbody measured in radians per second. (Default 7) range { 0, infinity }.

    The angular velocity of rigidbodies is clamped to maxAngularVelocity to avoid numerical instability with fast rotating bodies. Because this may prevent intentional fast rotations on objects such as wheels, you can override this value per rigidbody.
    EDIT: The Very Very Valet (for Nintendo Switch) guy made a REALLY cool vehicle physics tutorial here. No wheel colliders, just forces to simulate it all.

     
    Last edited: May 21, 2023
  6. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    I already modified the motor force... mass and other components. that didn't work though
     
  7. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    The thing is I have one rigid body on the car should I make separate ones per wheel colider?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I assumed you had ones on the wheels but looking at the WheelCollider API, it appears it is not necessary.

    Therefore I think my comment about max angular velocity DOES NOT apply in this case.
     
  9. a170982

    a170982

    Joined:
    Apr 20, 2021
    Posts:
    2
    how can my car turn left or right no matter forward or reverse? My tyre can turn left and right but when i turn my car wheel but my car cannot move either forward or reverse. I am doing using joystick(bluetooth controller for VR application). Here is my C# script.
     

    Attached Files:

    Last edited: May 9, 2021
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This has absolutely nothing to do with this topic. Please don't hijack old topics, it is against forum rules.

    Instead, start your own new post. It's FREE!

    When you post, it is critical that you make yourself understood properly.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855
     
    valentin56610 likes this.
  11. bloggerfala7

    bloggerfala7

    Joined:
    Jul 2, 2022
    Posts:
    1
    Hi, i see you problem right there, i don't know if you still need a solution for that or if you found one in 2 years, lol. So i had the exactly same problem as you and found out that it's because of the script, of course this is a common car controller script that many people use, but if you need more control over the acceleration you can use this script instead;
    --------------------------------------------------------------------
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class ACC : MonoBehaviour
    {
    public WheelCollider[] wheels;
    public float motorPower = 100;
    public float steerPower = 100;
    void FixedUpdate()
    {
    foreach (var wheel in wheels){
    wheel.motorTorque = Input.GetAxis("Vertical") * motorPower;
    }
    GetComponent<Rigidbody>().maxAngularVelocity = 10000;
    for (int i = 0; i < wheels.Length; i++)
    {
    if(i < 2)
    {
    wheels.steerAngle = Input.GetAxis("Horizontal") * steerPower;
    }
    }
    }
    }
    --------------------------------------------------------------------

    this script has the main things but also can be better, you can know how to get it better in this video
    by pablos lab.
     
  12. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    Thanks i might still use this!