Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by borkomk · Oct 29, 2013 at 05:50 PM · shootingcountertime.time

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image borkomk · Oct 29, 2013 at 09:52 PM 0
Share

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 :)

avatar image Dave-Carlile · Nov 04, 2013 at 11:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges