Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

RESOLVED: Struggling with a timer using Time.time

Discussion in 'Editor & General Support' started by PickyBiker, May 26, 2020.

  1. PickyBiker

    PickyBiker

    Joined:
    May 11, 2020
    Posts:
    9
    This game uses a timer to move a target every 10 seconds. The logic is:
    Code (csharp):
    1.  
    2. now = Time.time;
    3.  
    4. if (now - 10 > last)
    5. {
    6.     last = now;  // reset the timer
    7.     MoveTarget();
    8. }
    9.  
    As you can see, the 10 second cycle restarts immediately when the previous one expires. It can also have the time period reset whenever the ball gets close to the target with this code:
    Code (csharp):
    1.  
    2. distance = Vector3.Distance(target.transform.position, ball.transform.position);
    3.  
    4. if (distance < 1.1)
    5. {
    6.     last = now;  // reset the timer
    7.     MoveTarget();
    8. }
    9.  
    The problem is that when the timer is reset because the ball is close to the target, the result is multiple timer triggers. For example, let’s say the timer is reset at 5 seconds because the ball is close to the target, I would expect the timer to expire on every subsequent multiple of 5 seconds (5, 15, 25, 35 etc.). What I am actually getting is the timer expiring at every 5 second multiple AND every 10 second multiple (5, 10, 15, 20, 25 etc.)

    Can someone help me understand why this is happening?

    Here is the full script code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     //   private float xAngle, zAngle;
    8.     private GameObject ball, target, platform;
    9.     private float x, z;
    10.     private float now, last;
    11.     private float distance;
    12.  
    13.     void Update()
    14.     {
    15.         // move the platform according to the inputs
    16.         platform = GameObject.Find("Platform");
    17.         float xAngle = Input.GetAxis("Vertical") * .25f;
    18.         float zAngle = -Input.GetAxis("Horizontal") * .25f;
    19.         platform.transform.Rotate(xAngle, 0, zAngle, Space.Self);
    20.  
    21.         // see if it's time to move the target and lose a point
    22.         now = Time.time;
    23.  
    24.         if (now - 10 > last)
    25.         {
    26.             last = now;  // reset the timer
    27.             MoveTarget();
    28.         }
    29.  
    30.         // see if the ball is near the target and gain a point if it is      
    31.         target = GameObject.Find("Target");
    32.         ball = GameObject.Find("Ball");
    33.  
    34.         distance = Vector3.Distance(target.transform.position, ball.transform.position);
    35.  
    36.         if (distance < 1.1)
    37.         {
    38.             last = now;  // reset the timer
    39.             MoveTarget();
    40.         }
    41.     }
    42.  
    43.     void OnCollisionEnter(Collision col)
    44.     {
    45.         if (gameObject.name == "killZone")
    46.         {
    47.             // print("Detected collision between " + gameObject.name + " and " + col.collider.name);
    48.             // print("There are " + col.contacts.Length + " point(s) of contacts");
    49.             // print("Their relative velocity is " + col.relativeVelocity);
    50.             // Destroy(col.gameObject);
    51.             last = now;
    52.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    53.         }
    54.     }
    55.  
    56.     void MoveTarget()
    57.     {
    58.         if (target != null)
    59.         {
    60.             x = UnityEngine.Random.Range(-.42f, .42f);
    61.             z = UnityEngine.Random.Range(-.42f, .42f);
    62.             target.transform.localPosition = new Vector3(x, .46f, z);
    63.         }
    64.     }
    65. }
    66.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Do you have more than one instance of this script active?
     
  3. PickyBiker

    PickyBiker

    Joined:
    May 11, 2020
    Posts:
    9
    That is exactly what was wrong. Thank you!