• 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 /
  • Help Room /
avatar image
0
Question by Fewpwew130 · Sep 23, 2015 at 09:46 PM · timercasting

[C# attached] [screenshots attached] determining delta rate for UI casting bar

Hello, dear community! I would like to create a casting bar UI, and have found a wonderful tutorial here: http://www.youtube.com/watch?v=wtQ1ylRLZsA. However, I decided to write a script myself. It works almost as intended: the bar moves from start position to the end position smoothly no matter what time interval I set (1 second or 10 seconds doesn't matter). But it moves slightly more than it should.

Because of this - because the bar arrives always at exact time to the wrong position - it seems the problem lies in the way "rate" variable is calculated. Please have a look, for your convenience I am attaching C# script and screenshots. Your comments are welcome!

 using UnityEngine;
 using System.Collections;
 
 public class castingbarscript : MonoBehaviour {
 
 
     public GameObject castbar;
     public Vector3 startpos;
     public Vector3 endpos;
     public Vector3 Resultpos;
 
     public float timer1;
     public bool doing;
     public float rate;
 
     // Use this for initialization
     void Start () {
     
         startpos = castbar.transform.position;
     //    startpos = new Vector3 (0,0,0);
         endpos = new Vector3(castbar.transform.position.x - castbar.GetComponent<RectTransform>().rect.width, castbar.transform.position.y,castbar.transform.position.z);
     //    endpos = new Vector3 (155, 0, 0);
 
         Resultpos = startpos - endpos;
 
         doing = false;
         timer1 = 1f;
         rate = (Resultpos.x/timer1)*Time.deltaTime;
     }
 
 
     // Update is called once per frame
     void Update () {
         Debug.Log (castbar.transform.position);
         if (Input.GetKeyDown (KeyCode.J)) {
             doing = true;
 
         }
 
         //reset
         if (Input.GetKeyDown (KeyCode.L)) {
             doing = true;
             castbar.transform.localPosition = new Vector3 (0,0,0); 
             timer1 = 1f;
         }
 
 
         if (doing) {
             timer1 -= Time.deltaTime;
             if(timer1 >= 0){
             castbar.transform.position = new Vector3 (castbar.transform.position.x - rate,castbar.transform.position.y,castbar.transform.position.z); 
             //    castbar.transform.position = Vector3.Lerp(startpos,endpos, 0.5f);
         
             }
         }
     }
 }

alt text alt text

canvas1-1e.png (214.0 kB)
canvas1-2e.png (196.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fewpwew130 · Sep 24, 2015 at 04:20 PM

Found the error:

instead of: rate = (Resultpos.x/timer1)*Time.deltaTime; it should be: rate = Resultpos.x/timer1

and instead of: castbar.transform.position = new Vector3 (castbar.transform.position.x - rate,castbar.transform.position.y,castbar.transform.position.z); it should be: castbar.transform.position = new Vector3 (castbar.transform.position.x - rate * Time.deltatime,castbar.transform.position.y,castbar.transform.position.z);

here is the working script: using UnityEngine; using System.Collections;

 public class castingbarscript : MonoBehaviour {
 
 
     public GameObject castbar;
     public Vector3 startpos;
     public Vector3 endpos;
     public Vector3 Resultpos;
 
     public float timer1;
     public bool doing;
     public float rate;
 
     // Use this for initialization
     void Start () {
     
         startpos = castbar.transform.position;
     //    startpos = new Vector3 (0,0,0);
         endpos = new Vector3(castbar.transform.position.x - castbar.GetComponent<RectTransform>().rect.width, castbar.transform.position.y,castbar.transform.position.z);
     //    endpos = new Vector3 (155, 0, 0);
 
         Resultpos = startpos - endpos;
 
         doing = false;
         timer1 = 1f;
         rate = Resultpos.x/timer1;
     }
 
 
     // Update is called once per frame
     void Update () {
         Debug.Log (castbar.transform.position);
         if (Input.GetKeyDown (KeyCode.J)) {
             doing = true;
 
         }
 
         //reset
         if (Input.GetKeyDown (KeyCode.L)) {
             doing = true;
             castbar.transform.localPosition = new Vector3 (0,0,0); 
             timer1 = 1f;
         }
 
 
         if (doing) {
             timer1 -= Time.deltaTime;
             if(timer1 >= 0){
             castbar.transform.position = new Vector3 (castbar.transform.position.x - rate*Time.deltaTime,castbar.transform.position.y,castbar.transform.position.z); 
             //    castbar.transform.position = Vector3.Lerp(startpos,endpos, 0.5f);
         
             }
         }
     }
 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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 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

Getting a timer to go to 0 1 Answer

Cant get my Timer to add time. 2 Answers

System Timer main thread 0 Answers

Countdown timer... 2 Answers

How to make GameOve Appear when times'up in unity? 0 Answers


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