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

Time.deltaTime Problem

Discussion in 'Scripting' started by Fynn_GER, Jul 27, 2020.

  1. Fynn_GER

    Fynn_GER

    Joined:
    Jun 30, 2020
    Posts:
    9
    Hello
    i wrote a little clicker game where you click the screen and get a point now I am trying to add a auto clicker that adds every second one point, but now I cant click manually on the screen to get a point.
    Thanks for every answer!
    Thats my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ClickCount : MonoBehaviour
    7. {
    8.     public Text displayPoints;
    9.     public SafeAndLoad safeAndLoad;
    10.     private float Points;
    11.  
    12.     void Update()
    13.     {
    14.         Count();
    15.         displayPoints.text = "Points: " + safeAndLoad.points;
    16.     }
    17.     void OnMouseUp()
    18.     {
    19.         safeAndLoad.points += safeAndLoad.pointsPerClick;
    20.     }
    21.     void Count()
    22.     {
    23.         Points += Time.deltaTime * safeAndLoad.pointsPerTick;
    24.         safeAndLoad.points = Mathf.RoundToInt(Points);
    25.     }
    26. }
     
  2. Fynn_GER

    Fynn_GER

    Joined:
    Jun 30, 2020
    Posts:
    9
    Solved it by my own if somebody has the same problem heres the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ClickCount : MonoBehaviour
    7. {
    8.     public Text displayPoints;
    9.     public SafeAndLoad safeAndLoad;
    10.     private float Points;
    11.  
    12.     void Update()
    13.     {
    14.         Points += Time.deltaTime * safeAndLoad.pointsPerTick;
    15.         if(Points >= 1.0f)
    16.         {
    17.             safeAndLoad.points += 1;
    18.             Points = 0.0f;
    19.         }
    20.         displayPoints.text = "Points: " + safeAndLoad.points;
    21.     }
    22.     void OnMouseUp()
    23.     {
    24.         safeAndLoad.points += safeAndLoad.pointsPerClick;
    25.     }
    26. }
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I noticerd a small bug in your new code:
    Code (CSharp):
    1.         if(Points >= 1.0f)
    2.         {
    3.             safeAndLoad.points += 1;
    4.             Points = 0.0f;
    5.         }
    If points was, say 1.05, you're throwing out that extra .05. That means your points will accrue at a slightly slower rate than you expect. I would replace that code with this:
    Code (CSharp):
    1.         while (Points >= 1.0f)
    2.         {
    3.             safeAndLoad.points += 1;
    4.             Points -= 1f;
    5.         }
    With this code, if Points is 1.05, you will add 1 to your score and keep the leftover .05. This will also handle the case where you end up with e.g. 2.05 points, since it's a while loop.
     
    Fynn_GER likes this.
  4. Fynn_GER

    Fynn_GER

    Joined:
    Jun 30, 2020
    Posts:
    9
    Thanks! Thats great