Hello,
I have a problem that I unfortunately can’t solve on my own, any help will be appreciated!
Some part of the code is delayed when function is called from GUI.Button’s OnClick(). And it happens only in Android, in Unity Editor everything is fine.
I have a procedure attached to Player that adds direction, in which player should move, in Queue:
public void addDirection(int dir) // 1 = Left, 2 = Up, 3 = Right, 4 = Down
{
dirQueue.Enqueue(dir);
}
This procedure can be directly (identical) called from 2 places:
- GUI.Button script attached to sprites (DPAD)
- InputManager: swipe direction (mouse/touch)
When there is at least 1 element in the Queue, it is taken (Dequeue) and player is moved (MoveTowards) to destination gotten from the Queue .
Then I have a script on Camera which checks player’s position (destination position set from Queue) and if it differs from camera’s position, it moves camera to this position. Simplified:
Vector3 destPosition;
float speed;
bool _isCameraMoving;
void checkCameraPosition()
{
tempCameraPosition = PlayerController.Player._instance.player.destPos;
if(destPosition != tempCameraPosition)
{
destPosition = tempCameraPosition;
_isCameraMoving = true;
}
}
void moveToDest()
{
if (transform.localPosition != destPosition)
{
transform.localPosition = Vector3.MoveTowards(transform.localPosition, destPosition, speed * Time.deltaTime);
}
else
{
_isCameraMoving = false;
}
}
void Update()
{
if(!_isCameraMoving)
{
checkCameraPosition();
}
else
{
moveToDest();
}
}
As mentioned earlier, in Unity Editor everything works as expected.
Situation in Android is a little bit different:
- Swipe: Player and camera start to move simultaneously to the position from the Queue. As needed!
- Button: Player moves to destination position, but camera stays in place until player stops, only then camera moves to the position where player stopped.
Does anyone have any ideas what could be the cause for the difference among these platforms? Is it some fault of EventSystem?