Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why does Transform.INTERNAL_set_localRotation() have such an enormous performance impact?

Discussion in 'Scripting' started by xzbobzx, Feb 15, 2016.

  1. xzbobzx

    xzbobzx

    Joined:
    Apr 7, 2015
    Posts:
    29
    I'm working on a car game, custom made wheel physics. Each wheel has a bunch of gameobjects inside of it, and each wheel raycasts down to handle suspension. Grip and traction is determined by the orientation of the wheel.

    When I steer, the wheel turns, and this turning then causes the wheel to give different forces to the car, making it turn.

    I turn wheels with: wheel.localRotation = Quaternion.Euler( someVector3 );

    Now this is fine when you have a car, or maybe two. Each car calls it about 10 times or so. (Not sure how, each car only has two wheels that steer) Anyway, with a lot of multiple cars, something weird happens.

    The calculations really begin adding up. To the point where Transform.INTERNAL_set_localRotation() is called 132 times, and costs 14.36 ms.

    Another function, Quaternion.Euler(), also being called 132 times, only costs 0.06 ms.

    Why does Transform.INTERNAL_set_localRotation cost two-hundred and forty times as much as any other function?

    And more importantly, why does one method, called a bunch of times, take almost as long as an entire frame by itself?

    Anyone else got problems with this?
    And biggest of all, how do I optimize it?

    ---
    Edit: Additional notes:

    My wheels look like this:

    -Wheel (suspension script + grip script (raycasters))
    --SubWheel (wheel animation script)
    ---HandbrakeBall (box collider that enabled when the car stops)
    ---WheelModel (mesh renderer)
    ---Skidspawn (skidmark spawner)
    ----Particle effect
    ----Particle effect
    ----Particle effect
    --StopSuspension (sphere collider, keeps the car from falling through it's suspension)
    --Sounds (audio source)

    It's quite a hefty wheel, and the wheel model even used to have multiple sub mesh renderers, which lagged the wheel animation script with the exact same ransform.INTERNAL_set_localRotation() thing, in which case I was again local rotating a gameObject with child game objects.

    Thing is, removing child game objects from the wheel model was easy. They were just meshes. I could add them together in one big mesh. I, however, can absolutely not put the *entire* wheel inside one game object, that I would then rotate.
     
    Last edited: Feb 15, 2016
  2. jogus84

    jogus84

    Joined:
    Feb 8, 2015
    Posts:
    13
    Well, 1 year after I'm also experiencing this issue in my game.

    My current test scene is a setup with 32 "tank" objects, When tanks idle I'm at 5 ms per frame, when tanks start turning their turrets (via transform.localEulerAngles) update time increases to 10 ms! Profiler shows that 10% of all time is spent in Transform.INTERNAL_set_localRotation() with 32 calls.

    I must be missing something...
     
  3. ToshoDaimos

    ToshoDaimos

    Joined:
    Jan 30, 2013
    Posts:
    679
    Do you use PhysX? I heard its costly to manually transform when using rigid bodies.