[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);
}
}
}
}
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);
}
}
}
}
Your answer
Follow this Question
Related Questions
Play Audio with Timers 0 Answers
I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer
HH:MM:SS game clock with modifiable start time error 1 Answer
Timer issues when converting to minutes,hrs,days,months,years 0 Answers
How do I put a timer in RollerBall? 1 Answer