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 /
avatar image
0
Question by GaggiGamer · Oct 13, 2020 at 07:05 PM · wheelcollidercar gamecar physicswheelswheel colliders

How to automatically stop car when fuel ends up ?

So, I want to achieve a simple functionality here. On KeyCode.F pressed, the booster/fuel fills up and On KeyCode.W pressed (only once no need to hold) the car starts consuming booster/fuel. So far it's working good.

The Real problem I am facing is, to stop the car without pressing any key. I tried to add brakeTorque but i think that can't do this job. Any help will be appreciated..... (Sorry for bad english)

     void Update()
     {

         if (Input.GetKey(KeyCode.F))
         {
             StartCoroutine(FillBooster());
         }
         
         if (Input.GetKeyDown(KeyCode.W))
         {
             isMoving = true;
             StartCoroutine(ConsumeBooster());
 
         }
      }
 
 IEnumerator ConsumeBooster()
     {
         while (!isBoosterEmpty())
         {
             boostBar.fillAmount -= Time.deltaTime/2;
             SetMotorTorque(motorForce);
 
             print("boost left : " + boostBar.fillAmount)
             yield return null;
         }
         if (isBoosterEmpty())
         {
             ApplyBrake();
 
             yield return null;
         }
     }
 
  private void ApplyBrake()
     {
         print("is booster empty : " + isBoosterEmpty());
 
         // here I tried to use brakeTorque but I think it doesn't work that way. 
     }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by p3k07 · Oct 13, 2020 at 07:16 PM

Get rid of the while loop and just have

 IEnumerator ConsumeBooster()
 {
 boostBar.fillAmount -= 0.1f;
 SetMotorTorque(motorForce);
 yield return new waitforseconds(2);
 print("boost left : " + boostBar.fillAmount)
 if(boostBar.fillAmount <= 0)
 {ApplyBrake();}
 
 }


Keep in mind you will need to start the CoRoutine again after 2 seconds, so I'd suggest just using a normal void and -= using delta time, as you already have done.


edit: Try this instead.

 void Update()
 {
          
          
   if (Input.GetKeyDown(KeyCode.W))
   {
    isMoving = true;
    ConsumeBooster();
   }
 }
 
 void ConsumeBooster() 
 { 
   if(boostBar.fillAmount > 0) 
   {
     boostBar.fillAmount -= 2 * Time.deltaTime;
     SetMotorTorque(motorForce); 
     print("boost left : " + boostBar.fillAmount) 
   }
   else
   {
     ApplyBrake();
   }
 
 }



Comment
Add comment · Show 3 · Share
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 GaggiGamer · Oct 13, 2020 at 08:58 PM 0
Share

Thanks for your suggestion but I already mentioned I need help in stop the car automatically after the booster finishes up. And I'll consider your suggestions too, but I used while loop to get the smooth effect of getting the boostBar down and it's doing the work very effectively. Can you please suggest something to stop the car without pressing any key or something ?

avatar image p3k07 GaggiGamer · Oct 13, 2020 at 09:09 PM 0
Share

This should get what you want. It's called once when you press W and will continue to -0.1 from the fill every 2 seconds until depleted. Once < 0 the while loop will break and the ApplyBrake method is called.

If you want it to subtract the fuel once per frame, then replace the WaitForSeconds with WaitUntilEndOfFrame.

Edit - Also, check out this https://docs.unity3d.com/ScriptReference/WaitWhile.html

 IEnumerator ConsumeBooster()
 {
   while(boostBar.fillAmount  > 0)
  {
     boostBar.fillAmount -= 0.1f;
     Set$$anonymous$$otorTorque(motorForce);
     yield return new waitforseconds(2);
     print("boost left : " + boostBar.fillAmount)
 
     // While loop will break when fill < 0, but just to make sure.
     if(boostBar.fillAmount <= 0)
     { yield break;}
   }
   // This part of co-routine only accessed once when above while loop condition is false.
   ApplyBrake();
 }
avatar image GaggiGamer p3k07 · Oct 14, 2020 at 05:59 AM 0
Share

Apply brake is being called correctly but I'm not able to stop the car. Because i do't know about motorToque and brakeTorque much. I tried to apply brakeTorque but it didn't work. So, i want some suggestion about what should go inside this method.

avatar image
0

Answer by GrowingTurtle54 · Oct 13, 2020 at 07:18 PM

https://answers.unity.com/questions/1779420/how-do-i-test-my-multiplayer-game-with-friends.html Please help me with this

Comment
Add comment · Show 1 · Share
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 GrowingTurtle54 · Oct 13, 2020 at 07:23 PM 0
Share

please I dont know what to do

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

138 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

Related Questions

Strange Wheel Colliders 0 Answers

Wheel is wrong rotation 0 Answers

Problems with cars in Unity 5 0 Answers

Problem with cars in Unity 5 0 Answers

Does anyone know good free wheel collider alterantives? 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