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

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.

When the code in TestMover.cs is:

using UnityEngine;
using System.Collections;

public class TestMover : MonoBehaviour 
{
	void FixedUpdate()
	{
		transform.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
		// rigidbody.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
	}
}

The profiling result is:

When the code in TestMover.cs is:

using UnityEngine;
using System.Collections;

public class TestMover : MonoBehaviour 
{
	void FixedUpdate()
	{
		// transform.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
		rigidbody.position = new Vector3(Mathf.Sin(Time.time), 0, 0);
	}
}

The profiling result is:

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.

Why? Never use mesh colliders when you can use primitive colliders instead.

–Eric

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.

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.

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.

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)

:slight_smile:

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).