Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by DayyanSisson · Aug 22, 2011 at 08:26 PM · timespeedlimitboostregenerate

Limited Energy Regeneration

I'm making a Space Sim, and I have a boost script. The problem is, the player can boost for forever so I need to make it so that the boost can only be limited, and the boost starts to regenerate over time when the player isn't using boost. How should I do this with the existing script:

 var Speed : int = 50;
 var BoostSpeed : int = 200;
 var NormalSpeed : int = 50;   ///Must be same as Speed int
 
 function Update() 
 {
     transform.Translate(Vector3.forward * Time.deltaTime * Speed);
 
     if (Input.GetKeyDown ("space")){
     Speed = BoostSpeed;
 }
     if (Input.GetKeyDown ("space")){
 BoostSpeed = Speed;
 }
     if (Input.GetKeyUp ("space")){
 Speed = NormalSpeed;
 }
     if (Input.GetKeyUp ("space")){
 NormalSpeed = Speed;
 }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DayyanSisson · Oct 13, 2011 at 11:09 PM

I pretty much figured this out a while ago, but so this has an answer, here's the script with the limited energy:

 var starshipEnergy : float = 500;
 var energyLossRate : float = 100;
 var energyRegenerationRate : float = 25;
 var canBoost : boolean = true;
 var boostSpeed : float = 200.0; 
 var cruisingSpeed : float = 50.0;
 private var Speed : float = 50.0;
 private var fighter : boolean = false;

 transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
      
  
      if(!fighter){
      energyLossRate = 0;
      energyRegenerationRate = 0;
      }
  
      if(Input.GetKey("space") && starshipEnergy > 0 && canBoost){
      starshipEnergy -= energyLossRate * Time.deltaTime;        
      }
      else if(starshipEnergy <= 500 && canBoost){
      starshipEnergy += energyRegenerationRate * Time.deltaTime;
      }
      if(Input.GetKey("space") && starshipEnergy > 0 && canBoost)
      Speed = boostSpeed;
      else
      Speed = cruisingSpeed;

Hope this helps anyone with the same problem. You might need to change some variables to suit your own needs.

Comment
Add comment · 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
0

Answer by 3D-Magic-VR · Aug 22, 2011 at 09:09 PM

Hi, try to use this:

 var normalSpeed : float;
 var turboSpeed : float;
 var limitTurboTime : float;
 var maxturbo : float;
 var regen : boolean;

 function Awake () {
     limitTurboTime = maxTurbo;
 }
 
 function Update () {
     If (Input.GetKeyDown("space")) {
         normalSpeed = normalSpeed * turboSpeed;
         regen = false;
         if (limitTurboTime > 0)
             limitTurboTime -= Time.deltaTime;
     }
     if (Input.GetKeyUp("space")) {
         normalSpeed = normalSpeed/turboSpeed;
         regen = true;
     }
     if (limitTurbotime < maxTurbo && regen)
         limitTurboTime += Time.deltaTime;
 }

I think this should do the work.

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 DayyanSisson · Aug 23, 2011 at 12:54 AM 0
Share

Have you tried this yourself. First of all there are variable errors. Once you fix those, it doesn't exactly work. The speed doesn't change and the variable go crazy in the inspector.

avatar image
0

Answer by Rennat · Aug 23, 2011 at 04:42 AM

One way to achieve this would be to add some new variables:

 var CanBoost : boolean = true; // true or false can we boost rignt now?
 var BoostAmount : float = 1; // (0 to 1) amount of boost left
 var BoostDrainRate : float = 5; // seconds it should take for a full boost to drain
 var BoostRechargeRate : float = 10; // seconds it should take to recharge fully from no boost

then instead of using using GetKeyDown and GetKeyUp you can set the speed for that frame based on whether or not the boost key is pressed. You'll also probably want to use float instead of int not gonna get into why here but in this case float is probably closer to how you were thinking it should work.

Currently you are moving the GameObject before you update it's speed. This means that you're always using the previous frame's Speed value

 var Speed : float = 0; // starts at 0 but it's ok because we set it before we use it
 var BoostSpeed : float = 2;
 var NormalSpeed : float = 1;
 
 var CanBoost : boolean = true; // true or false can we boost rignt now?
 var BoostAmount : float = 1; // (0 to 1) amount of boost left
 var BoostDrainRate : float = 5; // seconds it should take for a full boost to drain
 var BoostRechargeRate : float = 10; // seconds it should take to recharge fully from no boost
 
 function Update () {
     /* 
      * first decide which speed to use 
      */
     if (CanBoost && Input.GetKey ("z")) {
         BoostAmount = Mathf.Max(0, BoostAmount - Time.deltaTime / BoostDrainRate);
         if (BoostAmount == 0)
             CanBoost = false; // we ran out of juice stop boosting
         Speed = BoostSpeed;
     } else {
         BoostAmount = Mathf.Min(1, BoostAmount + Time.deltaTime / BoostRechargeRate);
         if (BoostAmount == 1)
             CanBoost = true; // we're full we can boost again!
         Speed = NormalSpeed;
     }
      
     /*
      * then move 
      */
     transform.Translate(transform.forward * Speed * Time.deltaTime);
 }

i also noticed in your code you have multiple if statements for the same condition, you can combine them but in this case the statements you had inside the conditionals were redundant/unnecessary

Comment
Add comment · Show 13 · 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 DayyanSisson · Aug 23, 2011 at 05:12 PM 0
Share

You're right, there are some redundancies but I didn't bother to take them out. Your script works, but the problem is, it shoots me off diagonally backwards and to the right. And have no idea why. I realized even by making the variables negative, I would still move diagonally to the left. (The ship has no other scripts on it except for the mouselook.

avatar image Rennat · Aug 23, 2011 at 05:18 PM 0
Share

Is your ship oriented correctly so that it's forward is it's positive Z axis? This script uses the Transform's local coordinate system's forward (`transform.forward`) as a direction. You could try switching to using the World coordinates system's Z axis ins$$anonymous$$d by switching transform.forward for Vector3.forward.

avatar image DayyanSisson · Aug 23, 2011 at 05:28 PM 0
Share

It's oriented in a weird way, yes. It needs to be that way. Well I tried Vector3.forward, and I realized that in all my other code, it uses Vector3.forward, so it works... only when I'm not boosting. Once I start boosting, that's when the problems start. Same thing happens only when I boost.

avatar image Rennat · Aug 23, 2011 at 05:42 PM 0
Share

Yeah then the problem is the orientation of your object. A quick way to fix the orientation without making the object look different is to create a new GameObject aligned correctly and then just place your original rotated object inside. Then put scripts on the new GameObject.

In Unity positive x is always to the right, positive y is always up, and positive z is always forward. If you absolutely wanted to you could use an arbitrary direction as forward, however this means that every time you need to interact with that object and deal with rotation or direction then you need to take that rotation into account. In my experience it is never really worth it to go with a different direction as forward.

If you just cannot fix the ship's transform to point in the right direction then you will need to either calculate the actual forward or use something like Vector3.forward which is the World forward (if your ship turns at all then this wont work)

avatar image DayyanSisson · Aug 23, 2011 at 09:50 PM 0
Share

Well I made the orientation of my object straight along the z-axis, but when the ship is facing in another direction, it won't boost. According to your last comment, if the ship turns it won't boost correctly. Can I fix this, or should I just you another script.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Boost Script Time problems 1 Answer

Reset speed after a specified time 0 Answers

Speed limit? 2 Answers

Pathfinding with specific end-direction and turning speed limit 0 Answers

Is it possible to have a gameobject in it's own timeframe? 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