0
votes

I'm hoping someone can help me with the issue I'm having creating my 2D game. I'm using Unity to create an Android application. I'm moving my character (a goat) across the screen using a grid system (GridMove - http://wiki.unity3d.com/index.php/GridMove).

I included the Global and Move script in the following links :

Global : http://codeshare.io/i6BDn

Move : http://codeshare.io/JHDAs

In the move Update function there is a StartCoroutine which moves the goat to a certain position (based on the grid size which is 0.5).

The transform.position gets set with a Vector3.Lerp and the Time.deltaTime. On my computer it works fine, but when I start opening programs or attach the debugger the goat seems to keep looping in the same position. This also happens on a low end phone or even an Samsung Galaxy s4.

Is there a way to stop the goat from resetting his transform.position or a way to check it? I can't seem to pinpoint where it goes wrong.

enter image description here

Looping goat position : enter image description here

If you need more information, just let me know.

1
Please include your actual code in the question, and not a code share link.MX D
Do you really need that "yield return 0;" in "move" method?Volodymyr Baydalka

1 Answers

0
votes
while (t <= 1f)

Lets think about this for a second. You want to move him from position 0f to position 1f. If he reaches 1f, the code will not stop running because the condition of position == 1f still satisfies the while loop condition.

instead it should be like this:

float threshold = Single.Epsilon;
while ((1f - t) > threshold)

Bigger problem:

//Done with moving, set bool to false to "release" the update function
t = 0;
isMoving = false;
yield return 0;

Do not end your Ienumerator with a yield return 0. It does the exact same thing as yield return null, and will loop the IEnumerator all over again. You just reset your variables as well so it will start all over from position 0f. Instead you should end the IEnumerator by just terminating the code. It will not loop over again:

//Done with moving, set bool to false to "release" the update function
t = 0;
isMoving = false;