Countdown variable modified with deltaTime continuously being reset
Hi all,
I'm having an issue with deltaTime. I'm creating a script that limits a player's cursor speed so that there's a .2 second delay between when the player can move the cursor again, however delta time never seems to reduce my countdown. My outputs of player1MoveDelay
and player2MoveDelay
when I execute print
for each number are constantly somewhere around .82 - .83 for the number itself. What can I do to fix this?
I listed the code below, stripped of everything but the relevant lines.
Thank you.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SelectControl : MonoBehaviour {
public float MAX_VALUE = 0.2f;
public float player1MoveDelay = 0.2f;
public float player2MoveDelay = 0.2f;
// Use this for initialization
void Start () {
Time.timeScale = 1;
}
void checkMotionControls ()
{
//Player 1 controls
if (player1MoveDelay <= 0.0f) {
player1MoveDelay = MAX_VALUE; //then proceed
}
//Player 2 controls
if (player2MoveDelay <= 0.0f) {
player2MoveDelay = MAX_VALUE; //then proceed
}
}
// Update is called once per frame
void Update () {
checkMotionControls ();
checkTime (player1MoveDelay);
checkTime (player2MoveDelay);
}
void checkTime (float num)
{
if (num > 0) {
num -= (Time.deltaTime);
//for debugging purposes
print ("Time " + Time.deltaTime);
print ("Number " + num);
}
}
}
Here is an example of the output I will get:
Answer by Jessespike · Feb 05, 2016 at 06:20 PM
player1MoveDelay and player2MoveDelay never actually change. You're passing them into checkTime() which copies the value and stores it in the local variable "num".
You can pass by reference instead, I think that's what you were trying to do:
...
checkTime (ref player1MoveDelay);
checkTime (ref player2MoveDelay);
}
void checkTime (ref float num)
{
...
@HumblPi To specify a bit, this happens with all 'value types', such as Vectors, ints, Colors etc.
With a reference type you would actually be able to modify the object inside the function.
Thank you so much! I've been working on this thing for so long, tunnel vision set in and I missed something pretty obvious like that.
Your answer
Follow this Question
Related Questions
C# I need to move object in a cycle 0 Answers
Slow Motion Issues! 1 Answer
Time.deltatime not working. 1 Answer
How to Make Timer (Score) Count Up Faster? 1 Answer
Increase over time, 0 Answers