Peteris Ulmanis
Peteris Ulmanis

Reputation: 31

How to avoid lag with Unity MovePlayerOnYAxis in iOS?

My script works on Android very well and doesn't slow down, but when I try to use it on IoS sometimes it goes very good, and sometimes it lags and move player by 1 inch or less, and I don't know how to fix it. FPS doesn't go down, it stays stable at 60fps.

void MovePlayerOnYAxis()
    {

        foreach (Touch touch in Input.touches)
        {
            Vector3 newPosition = transform.position;
            newPosition.y += Mathf.Clamp(touch.deltaPosition.y * speed, MIN_SPEED, MAX_SPEED) * Time.deltaTime;
            transform.position = newPosition;

        }
    }

Upvotes: 0

Views: 285

Answers (1)

Pino De Francesco
Pino De Francesco

Reputation: 756

This code taken out of context is too little information to allow an actual answer, therefore here my opinion that will help you to investigate this better.

In my opinion the lag is hardly related to the OS handling touches, it's most likely an issue related to other operations you are performing at each frame. Android and iOS manage threads, rendering and memory very differently, so the simple fact that you mention 60FPS makes me think that you are hitting some sort of race condition: the optimal frame rate on mobile is 30, so I'd suggest to start the investigation by first limiting the frame rate putting the following in one of your scripts:

    void Awake() {
        Application.targetFrameRate = 30;
    }

That will leave enough compute power to the engine and the OS to perform other tasks, hence it's a good starting point in your investigation.

Upvotes: 1

Related Questions