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 /
This question was closed Feb 12, 2015 at 11:19 PM by Abacab for the following reason:

Other

avatar image
0
Question by Abacab · Feb 11, 2015 at 09:43 PM · c#buttoncar

Reverse Button problem

Hello!

I have a problem with my reverse button. It works if I use it at the start before pressing gas, but if gas is pressed even once then it doesn't work. Can someone spot my problem?

 void FixedUpdate ()
         {
                 currentSpeed = 2 * 22 / 7 * wheelBL.radius * wheelBL.rpm * 60 / 100;
                 currentSpeed = Mathf.Round (currentSpeed);
 
                 if (currentSpeed <= 0) {
                         reverseButton.GetComponent<CanvasGroup> ().alpha = 1;
                         reverseButton.GetComponent<CanvasGroup> ().interactable = true;
                         reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = true;
                 } else {
                         reverseButton.GetComponent<CanvasGroup> ().alpha = 0;
                         reverseButton.GetComponent<CanvasGroup> ().interactable = false;
                         reverseButton.GetComponent<CanvasGroup> ().blocksRaycasts = false;
                 }
 
                 if (gasOn) {
                         started = true;
                         Gas ();
                 }
 
                 if (reverseOn) {
                         Reverse ();
                 }
         }
 
 public void Gas ()
         {
                 motorInputTouch = 1;
                 gasOn = true;
 
                 wheelBR.brakeTorque = 0;
                 wheelBL.brakeTorque = 0;
         
                 // First gear
                 if (currentSpeed <= 150) {
                         wheelBR.motorTorque = OneTorque * motorInputTouch;
                         wheelBL.motorTorque = OneTorque * motorInputTouch;
                 }
                 // Second gear
                 else if (currentSpeed <= 300 && currentSpeed > 150) {
                         wheelBR.motorTorque = TwoTorque * motorInputTouch;
                         wheelBL.motorTorque = TwoTorque * motorInputTouch;
                 }
         
                 // Third gear
                 else if (currentSpeed <= 450 && currentSpeed > 300) {
                         wheelBR.motorTorque = ThreeTorque * motorInputTouch;
                         wheelBL.motorTorque = ThreeTorque * motorInputTouch;
                 } 
         
                 // Fourth gear
                 else if (currentSpeed > 450 && currentSpeed <= topSpeed) {
                         wheelBR.motorTorque = FourTorque * motorInputTouch;
                         wheelBL.motorTorque = FourTorque * motorInputTouch;
                 } else {
                         wheelBR.motorTorque = 0;
                         wheelBL.motorTorque = 0;
                 }
         }
     
         public void GasEnds ()
         {
                 motorInputTouch = 0;
                 gasOn = false;
 
                 wheelBR.brakeTorque = maxBrakeTorque;
                 wheelBL.brakeTorque = maxBrakeTorque;
                 wheelBR.motorTorque = 0;
                 wheelBL.motorTorque = 0;
         }
 
         public void Reverse ()
         {
                 motorInputTouch = 1;
                 reverseOn = true;
 
                 wheelFR.brakeTorque = 0;
                 wheelFL.brakeTorque = 0;
 
         if (currentSpeed <= 0 && currentSpeed > -maxReverseSpeed) {
                         wheelBR.motorTorque = OneTorque * -motorInputTouch;
                         wheelBL.motorTorque = OneTorque * -motorInputTouch;
                 } else {
                         wheelBR.motorTorque = 0;
                         wheelBL.motorTorque = 0;
                 }
         }
 
         public void ReverseEnds ()
         {
                 motorInputTouch = 0;
                 reverseOn = false;
         
                 wheelBR.brakeTorque = maxBrakeTorque;
                 wheelBL.brakeTorque = maxBrakeTorque;
                 wheelBR.motorTorque = 0;
                 wheelBL.motorTorque = 0;
         }
Comment
Add comment · Show 10
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
avatar image Dicen777 · Feb 11, 2015 at 10:10 PM 0
Share

At a quick glance, the only place I see reverseOn = true is in the Reverse function, which will never be reached should reverseOn = false Are you setting reverseOn = true somewhere else?

avatar image Abacab · Feb 11, 2015 at 10:12 PM 0
Share

Reverse function is called when I press the reverse button! :)

avatar image Dicen777 · Feb 11, 2015 at 10:23 PM 0
Share

I would check to make sure that currentSpeed is actually 0 or less when you are pushing the reverse button. It would make sense that may be the issue because I presume the car is stopped when you first start the game. If you hit the gas, is there anywhere after that where you know the currentSpeed would explicitly get set back to 0 or are you assu$$anonymous$$g it is by what you see?

avatar image Abacab · Feb 11, 2015 at 10:24 PM 0
Share

It is 0. Just checked from the inspector while running the game.

avatar image Dicen777 · Feb 11, 2015 at 10:40 PM 0
Share

I guess I would throw in some Debug.Log lines. One at the top of the Reverse function to make sure it's actually getting called, one after the if statement and one after you set the motorTorque. $$anonymous$$ake sure A) that it is getting to all of those lines and B) that the motorTorque properties are what you would expect them to be after they are set.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Car game Reverse Button 1 Answer

Button's sprite swap works fine but it doesn't change the related image's source image! why? 1 Answer

Key for GUI.Button 2 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