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.