Search code examples
unity-game-enginecameralagframe-rate

Unity3d - Moving camera literally cuts FPS in half?


I'm having an issue with the cameras in Unity. When the camera moves through any means it seems to cut my FPS in half if not more. It's not really noticeable on PC, unless I'm looking at the from from 800fps to about 150fps, however on mobile it'll cut the smooth 60fps I'm getting to 20fps on a Nexus 4. It's absolutely devastating.

Here's the properties for the camera I'm using & the script HOWEVER this issue still happens without ANY of these components and a completely reset camera component:

camera configuration

public class ViewDrag : MonoBehaviour
{
    public Vector3 hit_position = Vector3.zero;
    public Vector3 current_position = Vector3.zero;
    public Vector3 camera_position = Vector3.zero;
    public Vector2 min_position;
    public Vector2 max_position;
    float z = 0.0f;
    MouseHolder holder;
    hider sidebarHide;

    GameObject gameStructure;

    // Use this for initialization
    void Start()
    {
        gameStructure = GameObject.FindGameObjectWithTag("GameStructure");
        holder = gameStructure.GetComponent<MouseHolder>();
        sidebarHide = GameObject.FindGameObjectWithTag("SidebarBG").GetComponent<hider>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            hit_position = Input.mousePosition;
            camera_position = transform.position;

        }
        if (Input.GetMouseButton(0))
        {
            current_position = Input.mousePosition;
            LeftMouseDrag();
        }

        if (!sidebarHide.isHidden)
        {
            //GetComponent<Camera2DFollow>().enabled = true;
        }
        if(gameStructure.GetComponent<ExecuteMovement2>().isExecuted)
        {
            //GetComponent<Camera2DFollow>().enabled = true;
        }
    }

    void LeftMouseDrag()
    {
        // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
        // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
        //current_position.z = hit_position.z = camera_position.y;

        // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
        // anyways.  
        Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);

        // Invert direction to that terrain appears to move with the mouse.
        direction = direction * -1;

        Vector3 position = camera_position + direction;

        if (position.x < max_position.x && position.x > min_position.x && position.y < max_position.y && position.y > min_position.y)
        {
            if (!EventSystem.current.IsPointerOverGameObject() && holder.fromSlot == null )
            {
                if (sidebarHide.isHidden)
                {
                    if (!gameStructure.GetComponent<ExecuteMovement2>().isExecuted)
                    {
                        //GetComponent<Camera2DFollow>().enabled = false;
                        transform.position = position;
                    }
                }
            }
        }


    }
}

Has anyone got an idea why this happens and how I can work around it if not fix it?

Through closer inspection I think it has something to do with the Canvas being screen space.. But it's kind of needed that way. Again, any workaround?

Check comments for profiler screenshot.


Solution

  • Problem Solved:

    In the profiler I found that Canvas.SendWillRenderCanvases() was causing huge spikes. I solved the issue completely by turning off Pixel Perfect in the Canvas. Smooth as butter now.