- Home /
GameObject Array Shifting - Mobile Inaccuracies
Hi guys,
So I have a grid of game objects stored as GameObject[ , ]. I am shifting half of the grid up/down (left half if left was pressed, right half if right was pressed) based on up/down key presses. This is all working fine on my computer as I am running it, but it doesn't work properly when I put the build on my iOS/Android devices. The movements will be off by an amount each time I shift the grid.
EDIT: Just realised I didn't explain how I am doing the movements: I have tried two ways:
1 - Using Vector3.lerp in a coroutine to move each block independently in a main game script.
float startTime = Time.time;
while(Time.time < startTime + timeToTake)
{
gob.transform.position = Vector3.Lerp (from, to, (Time.time - startTime)/timeToTake);
yield return null;
}
gob.transform.position = to;
2 - Using Vector3.movetowards in an update loop running in a script on each object.
CellData targetData = gobGrid[gridIndexX, j].GetComponent<CellData>();
targetData.targetPosition = gobGrid[gridIndexX, j].transform.position - new Vector3(0, distanceToMove, 0);
targetData.distance = distanceToMove;
Image for better explanation:
The FPS doesn't drop below 30 when looping through the array and doing the movements.
Thanks,
Sospitas
So.. there isn't a single question in your post, also post your code, at least the relevant parts for heavens sake. There are any number of reasons why you are getting the behavior you're getting if your question is "why does it function differently on mobile then on my local computer". The CODE is the $$anonymous$$EY since it controls the view.
Sorry, forgot to actually include stuff cause I've been working all day. $$anonymous$$istakes happen. I've included it now
Thanks for including snippets, however, the values that you use to define the the positions are not posted in your code. variables like from, to, distanceTo$$anonymous$$ove... how these values are constructed are of the up most importance, include how you deter$$anonymous$$e the values.
I am so out of it right now. Sorry.
from = gobGrid[gridIndexX, j].transform.position; to = gobGrid[gridIndexX, j].transform.position + ( or - ) new Vector3(0, distanceTo$$anonymous$$ove, 0); (same as targetPosition in the second snippet). distanceTo$$anonymous$$ove = 2; (size in height of each grid object).
Number 1 is just a Coroutine that is run when an input is detected. The state is changed to its "movement" state so that inputs are no longer detected, and is only set back after the movements are finished.
Number 2 runs on each block/grid object. The CellData script that stores the targetPosition and the distance variables has an Update function that calls $$anonymous$$oveTowards ONLY if the game state is in the "movement" state using these variables and its current position.
After looking some more into this, I removed the Lerp, and just had the objects update to their target positions instantly. This removed the problem of the inaccurate movement, but it obviously doesn't look nice when the objects just teleport.
The Lerp code is acting on ~230 objects at once when it runs, but the FPS never drops below 30 (and is only below the normal average of 60 for the short Lerp duration).
Is there something bad about using Lerp on iOS/Android? If so, is there a better alternative that I should be using?