After doing a bit of testing here is what I've got: All tests were done at 800x600 resolution Video Sync turned on: The game runs at 60 FPS, 10% CPU usage, everything plays as it does when using the play function from Unity. Video Sync turned off: The game runs at 700 FPS, 50% CPU usage, the game plays very sluggish. The odd part is that when I tossed in a script to visually see what the velocity values of the main object, they are far less then what they were under the lower FPS test. So that leads me to believe that my script that uses rigidbody.AddForce in FixedUpdate to push a ball around is the problem. Anyone have any thoughts? Code (csharp): //Variables removed to save space function FixedUpdate () { if (parseInt (gameObject.transform.position.y * 100) <= upperCRange parseInt (gameObject.transform.position.y * 100) >= lowerCRange) { if (gameController.AcceptInput()) { rotationX += Input.GetAxis("Mouse X") * sensitivityX * -1; rotationY += Input.GetAxis("Mouse Y") * sensitivityY * -1; rotationX = Mathf.Clamp (rotationX, minimumX, maximumX); rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); originalXRotation = Mathf.Lerp (originalXRotation, rotationX, Time.deltaTime * damping); originalYRotation = Mathf.Lerp (originalYRotation, rotationY, Time.deltaTime * damping); xForce = maxForce * (originalXRotation / maximumX); yForce = maxForce * (originalYRotation / maximumY); gameObject.rigidbody.AddForce( Vector3 (xForce, 0, yForce), ForceMode.Force); } } } function Start () { gameController = gameObject.FindObjectOfType(GameController); }
I wonder if you're using both Update and FixedUpdate leading to some kind of frame-rate scaling issue -- a subtler variant is to use OnGUI (which triggers once per Update) to apply physics-based effects directly. You should just use FixedUpdate (and if you're using OnGUI etc. simply change hidden state which is then applied during a FixedUpdate to keep everything clean and simple). Fundamentally the difference between the two scenarios you're testing is that when you don't have video sync on there will be far more Update (and OnGUI) events and threads will get less time. You might also want to check to see if by "50%" it means that one CPU is maxed.
I would have vsync enabled unless the FPS drop below 30 where I would disable it through script. Additionally, add an FPS cap through setting the target fps, so the performance is focused on the world simulation, not on the rendering simulation
I totally agree with what dreamora said, but you may want to check for physics code being called from OnGUI or Update events (or threads), since this may be the underlying problem.
I found the issue and changed a function over to use FixedUpdate. The only problem now is what that code controls now feels sluggish, however CPU usage has dropped considerably. But, it is a start. Thanks for the help.
Maybe try to put that code in a coroutine thats gets started on levelLoad and runs indefinitly. Now you have something that behaves like FixedUpdate, only you control the delta Time. Maybe try half that of Time.fixedDeltaTime? Also why do you use parseInt? parseInt is for string, isn't it? Seems unnecessary to make floats to strings and then to ints. Try casting them directly to target type.