Trying to move buttons via Coroutine and transform.Translate
Hello, I'm trying to move the buttons to a specific position by pressing arrow keys. However, when I applied my code, the buttons moved nonstop. This is my code.
void Update () {
int horizontal = 0;
horizontal = (int) Input.GetAxisRaw("Horizontal");
if(horizontal == 1){
foreach(GameObject button in buttons){
Vector3 newPosition = button.transform.position - new Vector3(1f,0f,0f);
StartCoroutine(SmoothMovement(button,newPosition));
}
}
}
protected IEnumerator SmoothMovement(GameObject button,Vector3 end){
float sqrRemainingDistance = (button.transform.position - end).sqrMagnitude;
while (sqrRemainingDistance > float.Epsilon) {
button.transform.Translate(-inversedMoveTime*Time.deltaTime,0f,0f);
sqrRemainingDistance = (button.transform.position - end).sqrMagnitude;
yield return null;
}
}
I cannot figure out why this is not work. All buttons is on the canvas, I'am not sure if it have something to do with world and canvas space.
Comment
Your answer
Follow this Question
Related Questions
How to call a function every second ? 1 Answer
Flip 3D Character Rotation 180 on Y axis 1 Answer
Lerping Camera Between Two Points 0 Answers
Stupid question but I have to figure out 0 Answers
List update 0 Answers