- Home /
Android velocity slower than editor
I am trying to move multiple player GameObjects when input is received. Here is the code that I wrote to do this:
void FixedUpdate(){
if (movementVector!= curMovementVector) {
curMovementVector = movementVector;
for(int i = 0; i < players.Count; i++) {
Rigidbody2D rb = players[i].GetComponent<Rigidbody2D>();
PlayerController playerController = players[i].GetComponent<PlayerController>();
rb.velocity = movementVector * speed;
switch(movementDirection) {
case "up":
case "down":
playerController.SetSize(movementDirection);
rb.constraints = RigidbodyConstraints2D.FreezePositionX;
break;
case "left":
case "right":
playerController.SetSize(movementDirection);
rb.constraints = RigidbodyConstraints2D.FreezePositionY;
break;
}
}
}
}
The movement vector is set on input. I update the velocity only when a new input is received.
But whatever I do the speed of the players on my android phone is slower than in the editor. I have tried profiling for perfomance issues and also multiplying by both Time.deltaTime and Time.fixedDeltaTime. But nothing seems to fix the issue.
EDIT:
I tried inserting just hardcoded values, but it remained quicker in the editor.
This is how I get the movement vector and the speed is just a float:
void Update() {
if (globalVariables.isCreating == false) {
if (swipeControls.swipeUp) {
movementVector = transform.up;
movementDirection = "up";
}
if (swipeControls.swipeDown) {
movementVector = -transform.up;
movementDirection = "down";
}
if (swipeControls.swipeRight) {
movementVector = transform.right;
movementDirection = "right";
}
if (swipeControls.swipeLeft) {
movementVector = -transform.right;
movementDirection = "left";
}
}
}
Without code showing how movementVector and speed are calculated, there's almost nothing to offer.
If I assume speed is in meters per second and movementVector is influenced by user input, I could surmise that various platforms provide a metric of user input quite different from each other, which would require some "normalization" so as to unify the meaning of user input.
rb.velocity is an instruction to the physics engine to move the object for you over time, so indeed you wouldn't use deltaTime (or fixedDeltaTime - which doesn't mean what most think it means, according to the documentation - deltaTime is what code should use when that is required).
However, it isn't likely you must set velocity at every fixed update, but when user input implies a change to the velocity.
What is required is to debug by observing how different rb.velocity is being set on the two platforms, and since that comes from speed and movementVector, work backwards to where nd how those two are calculated until you see which parameter is different.
I would expect you haven't nailed down that the different observed speeds have factually used the same rb.velocity, right? Conduct and experiment to set rb.velocity to some fixed value that you can confirm works the same on both platforms.