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 Nercoe · Sep 27, 2012 at 02:55 PM · variabletimeover

Decrease a value over time when W is pressed.

Hey, what I am trying to achieve is this:

When the user presses the W key, I want the fuel variable to deduct by 10 every 5 seconds. I have written a little script but unfortunately for me, I'm only learning at the moment and can't seem to suss it out :P Here is my script:

 var speed : float = 3.0;
 var rotateSpeed : float = 3.0;
 var bulletPrefab:Transform;
 var shotDelay = 2;
 var status1 = "Loaded";
 var style : GUIStyle;
 var fuel : float = 100.0;
 var fuelDeduct = 2.0;
 
    function Start () {
 
     while (true) {
         while (!Input.GetButtonDown("Jump")) yield;
         bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, transform.rotation);
             bullet.tag = "bulletShot";
             status1 = "Reloading";
             bullet.rigidbody.AddForce(transform.forward * 5000);
         yield WaitForSeconds(shotDelay);
         status1 = "Loaded";
     }
 }
 
 function Update ()
 {
     var controller : CharacterController = GetComponent(CharacterController);
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
     var forward : Vector3 = transform.TransformDirection(Vector3.forward); var curSpeed : float = speed * Input.GetAxis ("Vertical");
     controller.SimpleMove(forward * curSpeed);
     
 
     }
 @script RequireComponent(CharacterController)
 
     if(Input.GetKey("w"))
     {
         fuel -= fuelDeduct * Time.deltaTime;
     }
 
 
 
 function OnGUI()
 {
         
         GUI.Label(Rect(250, 70, 100, 20), status1, style);
         GUI.Label(Rect(250, 120, 100, 20), fuel.ToString("f0"), style);
 }  
 

Basically I wish for the fuel variable to deduct by 5 every 10 seconds but so far nothing is happening. Can anyone point me in the right direction? :) Try not to spoon feed me please, I like learning the hard way :)

Comment
Add comment · Show 2
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 Muuskii · Sep 27, 2012 at 03:00 PM 0
Share

"You can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can."

avatar image Nercoe · Sep 27, 2012 at 03:10 PM 0
Share

I am just doing it in baby steps at the moment, I like to know everything I am writting. I have written this:

function Update () {

   while (Input.Get$$anonymous$$eyDown ("1")){

Debug.Log "hello"; } }

Why does that not return hello in the log?

2 Replies

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

Answer by Bunny83 · Sep 27, 2012 at 03:19 PM

Try something like this:

 var style : GUIStyle;
 var fuel : float = 100.0;
 var fuelDeduct = 2.0;  // reduce 2 each second == 10 every 5 sec
 
 function Update ()
 {
     if(Input.GetKey("w"))
     {
         fuel -= fuelDeduct * Time.deltaTime;
     }
 }
 
 function OnGUI()
 {     
     GUI.Label(Rect(250, 120, 100, 20), fuel.ToString("f0"), style);
 }

This will continously reduce the fuel while the button is held down. This is way better than reducing it in big intervals.

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 Nercoe · Sep 27, 2012 at 03:26 PM 0
Share

Thanks for the reply, I implemented your method but the variable does not decrease, could you point me in the right direction please? I will update the code.

avatar image MarkFinn · Sep 27, 2012 at 03:47 PM 1
Share

You've got Bunny's if statement in the wrong place. It needs to be inside the Update $$anonymous$$ethod.

avatar image Nercoe · Sep 27, 2012 at 03:50 PM 0
Share

Cheers $$anonymous$$ark, silly me :P Learning every day! Thank you very much also Bunny! Exactly what I needed :)

avatar image
0

Answer by Muuskii · Sep 27, 2012 at 02:59 PM

Is one of the rewrites a removal of a loop in Start()? Because otherwise it's never going to check for getkeyDown after the first check.

Keep in mind that the loop must reach a yield statement even if the player doesn't hit a key.

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

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

12 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

Related Questions

Delaying a dynamic Variable(transform.position for Ex.) 2 Answers

Triggering over time? 1 Answer

Played time? 2 Answers

Creating a variable jump using deltaTime in C#. 0 Answers

Adding 1 to a float over time to increase difficulty? 3 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