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
-2
Question by nichirob · Feb 23, 2014 at 08:04 PM · c#time.deltatimesprint

Time.deltaTime doesn't return number of seconds passed

Hello :) I have 2 scripts, my FirstPersonController.cs and my SprintLogic.cs. There's a static class in both of them called movementSpeed, which I'm trying to change in the Sprint Logic. I have a cooldown set for 3 seconds and the sprint duration also at 3. The problem is, using Time.deltaTime isn't helping me calculate the time passed. When I debugged it, it gave me numbers from 0.015 - 0.017. Is my code wrong? Help appreciated :) Also, I'm not quite sure how to change the cooldown. I want the player to be able to sprint for 3 seconds and have a cooldown of 2. How? :)

SprintLogic.cs

 using UnityEngine;
 using System.Collections;
 
 public class SprintLogic : MonoBehaviour {
     
     public static float movementSpeed;
     public float sprintSpeed = 10.0f;
     public float sprintDuration = 3.0f;
     private float sprintCooldown = 0f;
 
     CharacterController cc; //Sets CharacterController as "cc"
 
     // Use this for initialization
     void Start () {
         cc = GetComponent<CharacterController>();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKey(KeyCode.LeftShift) && cc.isGrounded && sprintCooldown <= 0) {
             FirstPersonController.movementSpeed *= 2;
             sprintCooldown += Time.deltaTime;
             
         }
         if (sprintCooldown >= 3) { //3 = sprint duration
             FirstPersonController.movementSpeed = movementSpeed / 2;
             sprintCooldown -= Time.deltaTime;
         }
 
         Debug.Log("Delta time: " + Time.deltaTime);
   
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by KiraSensei · Feb 23, 2014 at 08:07 PM

Time.deltaTime does not do what you want. It tells you how much time there was between the current frame and the last one.

See the documentation HERE.

What you need is explained in THIS question. Hope this helps !

Comment
Add comment · Show 16 · 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 nichirob · Feb 23, 2014 at 08:17 PM 0
Share

Awesome, but can you help me on where to put it in my script? :D

avatar image KiraSensei · Feb 23, 2014 at 08:24 PM 0
Share
 private float myTimer = 0.0f;
 
 void Update () {
  
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) && cc.isGrounded && sprintCooldown <= 0) {
             FirstPersonController.movementSpeed *= 2;
             sprintCooldown += Time.deltaTime;
             myTimer = Time.time; 
         }
         if (Time.time - myTimer >= sprintDuration) { //3 = sprint duration
             FirstPersonController.movementSpeed = movementSpeed / 2;
             sprintCooldown -= Time.deltaTime;
         }
  
         Debug.Log("Delta time: " + Time.deltaTime);
  
     }


I have let sprintCooldown because I don't know what is its purpose. It is possible that you don't need it anymore ...

avatar image nichirob · Feb 23, 2014 at 08:32 PM 0
Share

Thanks :) But wouldn't you're line:

 if (Time.time - myTimer ... ) {

be wrong? If we're setting myTimer to Time.time, wouldn't subracting it leave it as 0 since they're the same values?

avatar image KiraSensei · Feb 23, 2014 at 09:17 PM 0
Share

Only at the same frame you are hitting the key "shift". After the frame, this difference will increase. But this code is not perfect, the user can hit anytime he wants the shift key and will continue the sprint.

avatar image nichirob · Feb 23, 2014 at 09:52 PM 0
Share

I can't figure it out D: would you must posting a working script i'd be so happy! :)

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

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

Creating a timer for combat (C#) 1 Answer

Flip over an object (smooth transition) 3 Answers

How to keep my cursor inside window boundaries 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