Search Unity

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

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

2D physics performance

Discussion in 'Scripting' started by z37, Jan 26, 2015.

  1. z37

    z37

    Joined:
    Feb 13, 2014
    Posts:
    29
    Hi, everyone!
    Recently I came across an interesting article in my local language about performance issues that are connected with the nature of Unity physics engine, and how to solve them. link to article(rus)
    Three main unity physics laws that author claims are:
    The violation of these laws could result in massive performance drops because the engine marks the collider-violator as improper and initiates the re-initiation of such and the recalculation of phisics.

    1. Colliders should not move, rotate, scale or enabled/disabled.
    Ordinary collider is should be a static object like a tree or so.

    2. The movable object should always be a rigid body.
    As a rigid body does not initiate the entire recalculation of physics, only local, but it should be moved with the help of rigid body methods.

    3. If the object is a rigid body it should move only by rigid body methods.
    Forget about direct control of object movement through transform component if it has a collider attached. transform is a 'killer of performance'.
    Use rigid body methods instead:

    1. AddForce, AddTorque (the engine calculates the movement and collision depending on the mass of the body)
    2. Velocity and angularVelocity properties
    3. MovePosition, MoveRotation.
    (See the details in the documentation)

    The author also gives an example of 20box colliders movement and results in profiler.
    on the left transform.position / right rigidbody2d.MovePosition


    So the purpose of my post is to make a discussion of this topic, because some of the facts above were not obvious for me, and these mistakes do make difference on low-CPU devices.

    What do you think about that?
    P.S. what do Lerp method etc use for movement? and iTween?
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I'd post this to the physics forum.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,796
    I've never bench marked it, but these seem reasonably accurate to me.

    Its well documented that any collider that moves needs a rigidbody. Colliders without rigidbodys are considered static and baked into the engine for performance. While this dramatically improves performance, it becomes expensive every time you move one and have to recalculate the scene.

    To be honest I'd have to benchmark the transform one. This is the first I'd seen anyone complain about the cost of moving a rigidbody via its transform.
     
unityunity