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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Perfomance comparison. Transform, Rigidbody, Raycast.

Discussion in 'General Discussion' started by kidi0892, Nov 19, 2021.

  1. kidi0892

    kidi0892

    Joined:
    Mar 11, 2020
    Posts:
    4
    Hi.
    I made simplified tests to find out what is better way to move gameobjects around, performance-wise. Also I wanted to prove whether animation callbacks with raycast would perform better than usual colliders (for melee fight) and find out just how much it is more costly to cast raycast on complex mesh rather than on simple.

    TL;DR version - Raycasts are slightly more performant than colliders. Transform is slightly more performant than Rigidbody.

    Transform - move objects using transform.position
    Rigidbody - move objects using rigidbody.MovePosition

    I thought I would have moderate performance boost using raycasts instead of colliders but as it appeared the benefit is minimal.
    There is more clear benefit using transform over rigidbody even on complex gameobjects with lots of children. I saw earlier somewhere someone said that rigidbody would be faster on gameobjects with a lot of children than transforms so in my case that proved wrong.

    That info is not a 100% proof for what is better. I didn't use optimizations like jobs, GPU instancing, Graphics.DrawMeshInstanced etc.
    I found all I needed for myself. Hope that information will be useful for you.

    Any advices on errors, miscalculations in my tests or advices to make tests more "reality-applicable" are appreciated.

    use DOTS

    https://docs.google.com/spreadsheets/d/1gPkfsRBXME8kWP4cLJHs-LLL-ch-s3n9VjC9Fkob_NI/edit?usp=sharing - table with raw numbers.

    https://drive.google.com/file/d/1gzpqXyvefCQXi013fuS8r1LAi6mZrBCw/view?usp=sharing - project. All controls on Spawner class, spawner gameobject.
     

    Attached Files:

    Last edited: Nov 20, 2021
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
  3. kidi0892

    kidi0892

    Joined:
    Mar 11, 2020
    Posts:
    4
    I don't understand what you mean by that. I know that transform is... well, Transform. And Rigidbody uses Physics engine to move objects. That was the point of those simple tests. What would be more efficient to use: the former or the latter.

    I sense you implied something else though..
     
  4. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    I think what Neg is getting at is you should be more focused on what you want to do with it.

    If you are lobbing an object or doing a character controller that has little concern for constraints then rigidbody for sure.

    If you want something more game like, transform.

    And if you are doing projectiles raycasts all the way.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Those functions do not do the same thing. Because of that, there's no point of measuring their performance and comparing it, as the functionality is not the same, meaning each function has a specific purpose.

    For example, Transform moves any object, even if it is not part of physics engine.

    Rigidbody.MovePosition is used by kinematic rigidbodies to ensure a sudden movement does not make the scene explode. A rigidbody moved with "MovePosition" will be swept through the scene.

    Raycast has nothing to do with object position at all and instead is an almost direct query into physics system, and also its performance will be dependent on its length and number of objects in the world.

    Of COURSE they'll have different performance, but there's no point in comparing them to each other, because each one of those have a specific purpose and they're not really interchangeable. For example, if you need a raycast, you won't be able to replace it with a Transform.
     
    trombonaut and zombiegorilla like this.
  6. kidi0892

    kidi0892

    Joined:
    Mar 11, 2020
    Posts:
    4
    I see. I need to stress the part where I said that I used it for melee fights mostly.
    And I don't compare rigidbodies with raycasts or anything like. Sorry if my explanation was misleading. Here is video so that you can see what I test.



    In the first test on the video objects use colliders for collisions(simulating fight). In the second objects use raycast when closest to target(the real condition for raycast can be distance between objects or animation event). Both on video use transform to move around. Other tests include simple meshes, moving with rigidbodies etc.
     

    Attached Files: