Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am working on my first 2D game in Unity. And I have some struggles with camera, which seems lagy. It is like micro lags, when camera is folowing the players, sometimes. Should it be, because I used it in Update method?

My code looks like this:

void Update()
    {
        playerPosition = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
        if (player.transform.localScale.x > 0f)
        {
            playerPosition = new Vector3(playerPosition.x + offset, playerPosition.y, playerPosition.z);
        }
        else
        {
            playerPosition = new Vector3(playerPosition.x - offset, playerPosition.y, playerPosition.z);
        }
        transform.position = Vector3.Lerp(transform.position, playerPosition, offsetSmoothing * Time.deltaTime);
    }


What I have tried:

My thoughts are. That update method will make some actions every next frame. So if it is 110 fps, camera moved 110 times. Should I make it on lower fps, or put the code somewhere else, just not in Update method? Thank you.
Posted
Updated 2-Apr-20 18:20pm

1 solution

Use Stopwatch to narrow things down.

Stopwatch Class (System.Diagnostics) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900