How to detect changing values in update
Here is my code of passing gameobject to animate them. The animate code first scales gameobject and then returns back to original position
Here is the code where i pass gameobject to animate them.
for (int j = 0; j < gridSize; j++)
{
time = 0;
AnimatePlayObject(TileParent.transform.GetChild(fixedStart).transform.gameObject.GetComponent<Tile>().ObjectLocked);
fixedStart+=gridSize;
}
Here is the code for animating gameobject
private void AnimatePlayObject(GameObject go)
{
Vector2 finalScale = new Vector2(0.8f,0.8f);
Vector2 initialScale = new Vector2(0.5f, 0.5f);
if (time == 0)
{
go.transform.localScale = Vector2.MoveTowards(go.transform.localScale, finalScale, 0.005f);
if (go.transform.localScale.Equals(finalScale))
{
time = -1;
}
}
if (time == -1)
{
go.transform.localScale = Vector2.MoveTowards(go.transform.localScale, initialScale, 0.005f);
if (Mathf.Abs(go.transform.localScale.x - initialScale.x) < 0.02f && Mathf.Abs(go.transform.localScale.y - initialScale.y) < 0.02f)
{
time = 2;
}
}
}
I think the problem is both functions are in update function time is always set to zero .After the gameobjects reach finalScale it is not returning back to initialScale as written by code because time = -1 after it reaches finalScale.
Please someone help me to have time value updated to -1 after it reaches finalScale .Here time is always zero
Answer by Laiken · Sep 18, 2020 at 06:07 AM
Just to be sure, change line 10 to:
if (go.transform.localScale.x >= finalScale.x)
Also, for the cases like line 8, we normally do like this:
go.transform.localScale = Vector2.MoveTowards(localScale, finalScale, someSpeed * Time.deltaTime);
The reason is so that the fps won't affect the speed of the animation/movement
I want it to equal why is should i change to >= and thanks for the suggestion i will multiply with Time.deltaTime
Sorry I wasn't clear. I asked you to put '>=' just to be sure if the problem is that the 'go.transform.localScale' is actually not turning exactly equal to 'finalScale'.
But I read your code again. You said you were putting 'for (int j = 0; j < gridSize; j++)' inside the 'Update()' right? It has 'time = 0' inside it. Every frame it changes the time back to 0. What you need to do is remove the line 'time = 0' from there and put it outside the update.
You may want to consider using coroutines to handle cases like AnimatePlayObject btw