Having trouble moving a UI panel
Hi! I'm trying to code in a UI panel moving down, then up on screen at a certain time. However, the panel never moves. Here's the code I have if you can help, it's in C#:
void Start ()
{
speed = 1.0f;
Protag = GameObject.Find ("Protag");
player = Protag.GetComponent ("Player") as Player;
startPos = transform.position;
topPos = new Vector3 (Screen.width/2,(transform.localScale.y/2 + Screen.height/2), startPos.z);
bottomPos = new Vector3 (Screen.width/2, Screen.height * -1, startPos.z);
screenPos = new Vector3 (Screen.width / 2, Screen.height / 2, startPos.z);
}
// Update is called once per frame
void Update ()
{
float step = speed * Time.deltaTime;
if (countDown) {
timer += Time.deltaTime;
}
if (timer < 3.4 && timer > 3)
{
print("moving down");
transform.position = Vector3.MoveTowards(transform.position, bottomPos, step);
}
if (timer >= 3.4 && timer < 3.5)
{
print("moving up");
transform.position = Vector3.MoveTowards(transform.position, screenPos, step/4);
}
Thanks for the fix! The panel is moving now. However, where it ends up on screen is still different depending on if i have the game window small or maximized, even though everything is based on the panel and screen size. Is there a way to make sure it's movement always looks the same?
Edit: Never$$anonymous$$d. I'm dumb and didn't take all of your advise. Upping the timer gap fixed it. Not sure how to mark a comment as the correct answer, so if you copy your comment as one I'll mark it!
Answer by Umresh · Aug 24, 2015 at 10:47 AM
You speed value is small and the timer gap is also small. Try playing with the values. Movetowards will work as long as that condition is true. Or try this
if (timer < 3.4 && timer > 3)
{
Debug.Log("moving down");
transform.position = Vector3.Lerp(transform.position, bottomPos.position, step);
}
if (timer >= 3.4 && timer < 3.8)
{
Debug.Log("moving up");
transform.position = Vector3.Lerp(transform.position, screenPos.position, step/4);
}
YOu can place another panel and change its anchor point and position it. and get that panel as the bottom pos or the screenpos.