Search Unity

2D physics performance

Discussion in 'Physics' 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. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    transform.Translate gives MUCH better performance than adding force. It seems more efficient to simply set a new Vector3 position.
     
  3. z37

    z37

    Joined:
    Feb 13, 2014
    Posts:
    29
    I agree with you.
    But the author of the article, said that for the purposes of his game it was more efficient to move the level in his game using MovePosition, MoveRotation.

    He said that he had really bad performance on low-CPU devices with transform methods, but after swapping to MovePosition, MoveRotation he got a growth in 30 fps.
    The only thing that bothers me is that low-level rigid body control methods does not bring a smoothness in movement as of the nature of it.
     
  4. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    You're the author :D
    Btw that game in your post seems SUPER cool! If it's yours, i recommend a little damping of the plane... give the camera some momentum.
     
  5. Ander_zh

    Ander_zh

    Joined:
    May 18, 2017
    Posts:
    2
    hi bro, "that low-level rigid body control methods does not bring a smoothness in movement as of the nature of it." i also had met this probole ,i use move rotatioin , the robot head shake ...
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Note: You're actually replying to a post from 2015! Nevertheless I thought it worth replying myself for anyone else jumping in and wondering the same.

    When you add a Rigidbody2D you are asking it to take control of the Transform; it effectively becomes a proxy to the Transform so never ever modify the Transform. Ever.

    To change the pose, you go via the Rigidbody2D API i.e. adding forces, setting the velocities or using MovePosition/MoveRotation.

    Modifying the Transform is supported but only because devs do it. It's not recommended and is worse for performance. Also, it is immediately teleporting the body from one position to another and can cause collision tunneling and collider overlaps. It also stops interpolation from working.

    Also, don't try to move a Collider2D that isn't attached to a Rigidbody2D. A collider like this is static meaning never moving. If you are going to move it then add a Rigidbody2D and set it to Kinematic body-type. If you are going to hardly ever move it then it's also fine to do the same but use a Static body-type.

    In the end, Rigidbody2D move, colliders don't. Collider geometry lives in Rigidbody2D space.
     
    Torbey likes this.