Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NEVER NEVER use transform.position on GameObject with rigidbody and many colliders

Discussion in 'Scripting' started by ShenYuan, Dec 29, 2013.

  1. ShenYuan

    ShenYuan

    Joined:
    Aug 27, 2012
    Posts:
    5
    This is a test I just made on PC, which solve the mysterious lag of our game on mobile device.
    Because I haven't seen any thread talked about this, so I think I need to post this one.

    This is my scene setup, every "Cube" gameobject is a convex mesh collider.
    $2013-12-29 16-58-51.png

    When the code in TestMover.cs is:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestMover : MonoBehaviour
    6. {
    7.     void FixedUpdate()
    8.     {
    9.         transform.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
    10.         // rigidbody.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
    11.     }
    12. }
    13.  
    The profiling result is:
    $2013-12-29 16-25-35.png

    When the code in TestMover.cs is:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestMover : MonoBehaviour
    6. {
    7.     void FixedUpdate()
    8.     {
    9.         // transform.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
    10.         rigidbody.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
    11.     }
    12. }
    13.  
    The profiling result is:
    $2013-12-29 16-52-38.png


    In conclusion, set transform.position on GameObject with rigidbody and many colliders will cause huge performance impact.
    I know this is hard and need to change code, because set rigidbody.position wont affect instantly. But check this huge CPU cost, I think it will definitly change your mind. Don't forget this time cost will grow 10 times bigger on a mobile device.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Why? Never use mesh colliders when you can use primitive colliders instead.

    --Eric
     
  3. HungrySnake

    HungrySnake

    Joined:
    Dec 27, 2013
    Posts:
    16
    Eric is right on that. Mesh colliders do cost more performance than just primitive colliders, like a box collider, which seems more appropiate for this.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It is just a test or reproduction of the issue. It is very likely that the actual project requires mesh colliders. Of course they should be avoided if possible, but that doesn't eliminate the surprisingly big performance impact.
     
  5. ShenYuan

    ShenYuan

    Joined:
    Aug 27, 2012
    Posts:
    5
    Thanks for u to point this out, but as Dantus said. This is a test, so to make sure cost change is significant enough, I use mesh collider instead of primitive collider. Anyway, in real game project, it's rare to setup primitive collider for every single prefab. It's often to use convex mesh which auto generated directly from render mesh by default, when the performance problem comes(often occurred at late stage of development), change some of them into primitive collider.
     
  6. ShenYuan

    ShenYuan

    Joined:
    Aug 27, 2012
    Posts:
    5
    And thanks u Dantus to help me explain this. I've read a lot of your posts before, which really helped me a lot.

    BTW, I've bought your cloud system and using your free Decal System, real real good mid-ware indeed! It's real nice to see your reply 8)
     
    Last edited: Dec 29, 2013
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    :)
     
  8. dreasgrech

    dreasgrech

    Joined:
    Feb 9, 2013
    Posts:
    205
    Using ridbody.position (along with MovePosition()) won't be sufficient if you're using the parent rigidbody as a moving platform with child rigidbodies (players' characters).