Search Unity

Performance Problems

Discussion in 'Scripting' started by rcalt2vt, Jul 12, 2009.

  1. rcalt2vt

    rcalt2vt

    Joined:
    Jun 6, 2009
    Posts:
    36
    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):
    1.  
    2. //Variables removed to save space
    3.  
    4.     function FixedUpdate () {
    5.         if (parseInt (gameObject.transform.position.y * 100) <= upperCRange  parseInt (gameObject.transform.position.y * 100) >= lowerCRange) {
    6.             if (gameController.AcceptInput()) {
    7.                 rotationX += Input.GetAxis("Mouse X") * sensitivityX * -1;
    8.                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY * -1;
    9.                
    10.                 rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
    11.                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    12.                
    13.                 originalXRotation = Mathf.Lerp (originalXRotation, rotationX, Time.deltaTime * damping);
    14.                 originalYRotation = Mathf.Lerp (originalYRotation, rotationY, Time.deltaTime * damping);
    15.                
    16.                 xForce = maxForce * (originalXRotation / maximumX);
    17.                 yForce = maxForce * (originalYRotation / maximumY);
    18.                
    19.                 gameObject.rigidbody.AddForce( Vector3 (xForce, 0, yForce), ForceMode.Force);
    20.             }
    21.         }
    22.     }
    23.    
    24.     function Start () {
    25.         gameController = gameObject.FindObjectOfType(GameController);
    26.     }
     
  2. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    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.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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
     
  4. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    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.
     
  5. rcalt2vt

    rcalt2vt

    Joined:
    Jun 6, 2009
    Posts:
    36
    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.
     
  6. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    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.