- Home /
Creating an up and down counter
Hi i am trying to make a counter that counts 70 and down to 0 and up to 70 again and so on. The use for the counter is that i have canon the shoots on touch, the speed of the ball is the current value of the counter. This the code that i have so far:
 using UnityEngine;
 using System.Collections;
 
 public class Pukanje : MonoBehaviour {
     
     public GameObject proektil;
     public float fireRate = 0.5f;
     internal float nextFire;
     public int speed;
     public AudioClip weaponFX;
     
     public GUIText brzina;
     
     
     
     // Update is called once per frame
     void Update () 
     {
         speed = (int)Time.time;
         
         
             int max = 120;
             
             if(speed > 60)
             {
             speed = max - (int)Time.time;
             
                 
             }
             if (speed < 0)
             {
                 speed = Mathf.Abs(speed);
             }
         
         
         
         brzina.text = speed.ToString();
         // Fire is the left mouse button
         
         if (Input.GetButtonUp ("Fire1") && Time.time > nextFire) 
         {
             nextFire = Time.time + fireRate;
             GameObject clone = Instantiate(proektil,transform.position,transform.rotation) as GameObject;
             
             clone.rigidbody.velocity = transform.TransformDirection(-speed,0,0);
             
             audio.PlayOneShot(weaponFX);
             
             DestroyObject(clone,5);
         }
     }
 }
The speed variable stores time.time value. Than it checks if it is greater than 60 if it is it begins counting backwards by subtracting from the max variable. And to go up again takes the absolute value of the now negative values of the counter. But than just keeps going up. The rest of the script is the fire function. Thanks in advance for any help.
Answer by borkomk · Nov 05, 2013 at 12:05 AM
I did it Unity has a function Mathf.PingPong.This is the solution
     public float timerce;
     public GUIText tekst;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
        timerce = Mathf.PingPong(Time.time, 20);
        tekst.text = timerce.ToString();
     }
Answer by Dave-Carlile · Oct 29, 2013 at 05:58 PM
Something like this should work...
 float speed = 0;
 float direction = 1;
 speed += Time.deltaTime * direction;
 if (speed < 0 || speed > max)
 {
   direction = -direction;  // reverse direction
   speed = Mathf.Clamp(speed, 0, max); // make sure we stay within the bounds
 }
Basically, use a direction variable that keeps track of the direction, either 1 or -1. When adding a value to speed, always multiply that value by direction so it's positive or negative accordingly. When you get to one of the extents, reverse direction by negating it - i.e. direction = -direction.
Also, you're using Time.time, which gives the time since the game started. You likely want to use Time.deltaTime instead.
Tried it. Freezes at 60 i.e the max value.direction doesn't kick in. And i cant convert time.deltatime to int. I am a nweeb in unity :)
Sorry, I didn't see your comment. I'm not sure what you're issue is, but that code is working for me. Here's an entire class that implements it, along with logging...
 using UnityEngine;
 using System.Collections;
 
 public class Test : $$anonymous$$onoBehaviour {
 
     float max = 10;
     float speed = 0;
     float direction = 1;
     float lastSpeed;
 
     void Update () 
     {
         speed += Time.deltaTime * direction;
 
         // log speed each second
         if ((int)lastSpeed != (int)speed)
         {
             Debug.Log(string.Format("New Speed: {0}", speed));
             lastSpeed = speed;
         }
 
         if (speed < 0 || speed > max)
         {
             Debug.Log("Change direction");
             direction = -direction;  // reverse direction  
             speed = $$anonymous$$athf.Clamp(speed, 0, max); // make sure we stay within the bounds
 
         }
 
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
ammo counter, counts wrong 1 Answer
how do i stop automatic firing? 3 Answers
Run time counter 1 Answer
Only tagged objects raise counter 1 Answer
Making something happen only for one second during an Update function? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                