Search code examples
unity-game-engineunity-editor

In game view, the camera speed is perfect but while testing in phones the camera speed is much slower


In my game, the camera speed is perfect as I wanted it to be.

In phones, while testing my game camera speed is dead slow. I don't know how to fix this and what is the root cause of this problem.

This is the script which I attached to the main camera.

public float translation;
public float highspeed;//highest speed of the camera
public float incfactor;//increasing ,multiplying number
public bool ismoving = false;

private float timer = 0f;
private Rigidbody2D dia;

private void Start()
{
    dia = GetComponent<Rigidbody2D>();
}

private void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
        if (hit.collider != null)
        {

            if (hit.collider.tag == "dialogue")
            {
                Destroy(hit.collider.gameObject);
                ismoving = true;
            }
        }
    }

    if (ismoving == true)
    {
        Updatemove();
    }
}

public void Updatemove()
{
    timer += Time.deltaTime;

    if (timer > 1f && translation < highspeed)
    { //after 1 second has passed...
        timer = 0; // reset timer
        translation += incfactor; //increase speed by 0.5
    }

    transform.Translate(0, translation, 0);
}

Solution

  • You are calling Updatemove in Update which is called every frame.

    But than you Translate by a fixed value translation which is frame-dependent (lower framerate => less calls => slower movement)

    You're object will move with a speed of (translation * framerate) / seconds

    in order to eliminate that framerate factor and get a stable, device independent speed of translation / seconds you have to multiply by Time.deltaTime.

    so it should rather be

    public void Updatemove() 
    {   
        timer += Time.deltaTime;
    
        if (timer > 1f && translation < highspeed) 
        { //after 1 second has passed...
            timer = 0; // reset timer
            translation += incfactor ; //increase speed by 0.5
        }
    
        transform.Translate(0, translation * Time.deltaTime, 0);
    }
    

    since Time.deltaTime is a quite small value (1/framerate = 0.017 (for 60 fps)) you will probably have to increase your incfactor more or less by a factor of 60