- Home /
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);
}
}
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 !
Awesome, but can you help me on where to put it in my script? :D
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 ...
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?
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.
I can't figure it out D: would you must posting a working script i'd be so happy! :)
Your answer
Follow this Question
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