- Home /
Player movement speed significantly different on Windows build vs. Android build (Using Time.deltaTime)
I'm making a top-down 2D game mainly for Android. When clicked or touched (depending on platform), player should move to the selected location with constant speed (no acceleration nor deceleration).
Current code for this has been working just fine, but the movement speed is significantly different on Editor/Windows build compared to Android build, which is the target platform. This causes a great headache currently as the movement speed is altered different ways during the game play and there has to be better way than setting different speed scaling factors depending on the platform..
I have tried to search answers and found few topics related to this kind of an issue, but the solutions for those has been, in almost every case, using Time.deltaTime/Time.smoothDeltaTime in Update funtion. And I have been using it already form the very beginning..
I have tried two ways to implement this movement functionality:
1) in Update function:
float moveStep = 1 / (currentMoveDuration * Time.smoothDeltaTime * 100f);
Vector2 newPosition = Vector2.MoveTowards(rb2D.position, endPoint, moveStep);
rb2D.MovePosition(newPosition);
2) in Update function:
float distanceScale = Vector2.Distance(rb2D.position, endPoint);
float moveStep = 1 / (currentMoveDuration * distanceScale * 100f);
Vector2 newPosition = Vector2.Lerp(rb2D.position, endPoint, moveStep);
rb2D.MovePosition(newPosition);
moveStep calculation is as it is because I tried to find nice range to alter parameter "currentMoveDuration".
These two ways result in similar behavior in game, but the issue remains. I have nicely adjusted and tested values for moveDuration in Editor, but when I build the game on Android, player moves like a snail. If I change the formula for moveStep to its inverse, that is, "1 / (currentMoveDuration distanceScale 100f)" to "currentMoveDuration distanceScale 100f", then snail becomes skyrocket, as expected.
Input is acquired as follows (in Update):
if UNITY_EDITOR || UNITY_STANDALONE
if (Input.GetMouseButton(0) && !isDead && !pointerOnButton)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
else
if ((Input.touchCount == 1 && (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved)) && !isDead && !pointerOnButton)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
endif
worldPoint is basically endPoint in the movement code.
Also a side note: I'm using Time.deltaTime for other functionalities like moving projectiles and those work just fine. So I really don't know why it does not work when moving player.
I really appreciate if anyone has any ideas to solve this.
-WreaDn
Answer by WreaDn · Oct 15, 2017 at 06:55 PM
I ended up solving the issue myself after studying Unity reference in detail and doing some trial and error testing.
Basically the issue was that I used RigidBody2D.MovePosition() in Update() function with Time.deltaTime although it should have been in FixedUpdate() with Time.fixedDeltaTime.
Now player, with dynamic RigidBody2D, is moving with a constant speed regardless of the current FPS (On Editor/Windows, FPS is ~200 while on Android FPS is significantly lower).
This is the movement code now in FixedUpdate():
float moveStep = 1 / (currentMoveDuration * Time.fixedDeltaTime * 25f);
Vector2 newPosition = Vector2.MoveTowards(rb2D.position, endPoint, moveStep);
rb2D.MovePosition(newPosition);
Once again, the formula for moveStep calculation is as it is because I tried to find a good range for moveDuration variable. Basically move speed should be multiplied by Time.fixedDeltaTime.