Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CyberModeSoftware · Dec 20, 2020 at 07:43 PM · physicscarmotortorque

Why the car gears dosen't work.

I have a big problem with my car script , the gears is not working . I want a automatic gearbox but my function who change gears dosent want to update the gears just if I call in in the update function. But when I call in in the update the gear jump to 4th gear not in to 2nd.(he jump at the last gear from array)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 
 
 
 public class CarController : MonoBehaviour
 {
 
 
     public AnimationCurve EnginePower;
     public Button upshift;
 
     public steeringwheel2 Steering;
     public GameManager Manager;
     public Slider slider;
 
     public Rigidbody car_rb;
     public double currentspeed;
 
     public int[] gearSpeeds = new int[] { 40, 80, 120, 160, 220 };
     public float[] gears;
     public int gear = 1;
     [HideInInspector] public bool reverse = false;
 
     public float m_horizontalInput;
     public float m_verticalInput;
     public bool isbrake = false;
 
 
     public float DownForceValue = 10f;
 
     public float wheelsRPM;
     public float totalpower;
     public float maxRPM;
     public float engineRPM;
 
     
 
 
     public void Steer()
     {
         m_steeringAngle = Steering.GetAngle() / maxSteerAngle;
         frontDriverW.steerAngle = m_steeringAngle;
         frontPassengerW.steerAngle = m_steeringAngle;
     }
 
     public void Accelerate()
     {
 
         if (currentspeed <= 220)
         {
 
 
             if (m_verticalInput == 1)
             {
                 frontDriverW.motorTorque = totalpower;
                 frontPassengerW.motorTorque = totalpower;
                 rearDriverW.motorTorque = totalpower;
                 rearPassengerW.motorTorque = totalpower;
             }
             else if (m_verticalInput == 0)
             {
                 frontDriverW.motorTorque = 0;
                 frontPassengerW.motorTorque = 0;
                 rearDriverW.motorTorque = 0;
                 rearPassengerW.motorTorque = 0;
             }
         }
     }
 
     public void If()
     {
         if (m_verticalInput == -1)
         {
             Brake();
         }
         else if (m_verticalInput == 0)
         {
             NotBrake();
         }
     }
 
     public void Brake()
     {
 
 
         frontDriverW.brakeTorque = brakePower;
         frontPassengerW.brakeTorque = brakePower;
         rearDriverW.brakeTorque = brakePower;
         rearPassengerW.brakeTorque = brakePower;
     }
 
     public void NotBrake()
     {
         frontDriverW.brakeTorque = 0;
         frontPassengerW.brakeTorque = 0;
         rearDriverW.brakeTorque = 0;
         rearPassengerW.brakeTorque = 0;
     }
 
    
     public void shifter()
     {
         if (engineRPM > maxRPM && gear < gears.Length - 1)
         {
             gear++;
         }
     }
 
 
     private void UpdateWheelPoses()
     {
         UpdateWheelPose(frontDriverW, frontDriverT);
         UpdateWheelPose(frontPassengerW, frontPassengerT);
         UpdateWheelPose(rearDriverW, rearDriverT);
         UpdateWheelPose(rearPassengerW, rearPassengerT);
     }
 
     private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
     {
         Vector3 _pos = _transform.position;
         Quaternion _quat = _transform.rotation;
 
         _collider.GetWorldPose(out _pos, out _quat);
 
         _transform.position = _pos;
         _transform.rotation = _quat;
 
 
     }
     public void Calculatingenginepower()
     {
         WheelRpm();
         
         
         float velocity = 0.0f;
         totalpower = EnginePower.Evaluate(engineRPM) * (gears[gear]);
         engineRPM = Mathf.SmoothDamp(engineRPM, 1000 + (Mathf.Abs(wheelsRPM) * 3.6f * (gears[gear])), ref velocity, 0.09f);
 
 
         
     }
     public void WheelRpm()
     {
         wheelsRPM = frontDriverW.rpm + frontPassengerW.rpm + rearDriverW.rpm + rearPassengerW.rpm / 4;
 
 
 
     }
 
     public void DownForce()
     {
         car_rb.AddForce(-transform.up * DownForceValue * car_rb.velocity.magnitude);
     }
     public void Update()
     {
         Steer();
         UpdateWheelPoses();
         Accelerate();
         Calculatingenginepower();
         DownForce();
         WheelRpm();
         
         currentspeed = car_rb.velocity.magnitude * 3.6;
     }
 
     
 
 
 
 
 
 
     private float m_steeringAngle;
 
 
     public WheelCollider frontDriverW, frontPassengerW;
     public WheelCollider rearDriverW, rearPassengerW;
     public Transform frontDriverT, frontPassengerT;
     public Transform rearDriverT, rearPassengerT;
     public float maxSteerAngle = 30;
     public float motorForce = 500;
     public float brakePower = 100000;
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

226 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Car steering on a Rigidbody (Mario Kart) 0 Answers

How to make physics for rail trolley with gravity changing. Unity 2d game 0 Answers

Help with drift physics 0 Answers

How to make handbrake turn or powerslide with wheel colliders? 0 Answers

About Car Physics 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges