Search Unity

Question Applying forces from within C# jobs ?

Discussion in 'Scripting' started by The_Storm_Rider, Jul 1, 2022.

  1. The_Storm_Rider

    The_Storm_Rider

    Joined:
    Jan 28, 2020
    Posts:
    66
    Hello,

    I have a buoyancy code that calculates the forces on each triangle of the collider mesh. I want to optimize it with C# job, all the calculations per triangle are independent and should be easily implemented in multi-threading.

    So I'm learning C# jobs, and this question arises : is it possible to apply a force on the rigidbody from within a job ? If yes, how can we reference the rigidbody in the Execute() ? If not, should I store all the force in a native array and loop again in the main thread to apply the forces ? Doesn’t seem very optimized.
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    You can't use component references in C# jobs. A job can only access blittable data types.
    Unity does have
    IJobParallelForTransform
    which I tried to use for projectiles. But that experiment lead me to other performance problems due to managing the
    TransformAccessArray
    .
    So in my own experience experimenting with C# jobs using GameObjects is more annoyance than performance.
    I might've done something wrong perhaps.

    ECS works a lot better with the C# job system, but ECS is still experimental and also a whole different way of doing things.
     
  3. The_Storm_Rider

    The_Storm_Rider

    Joined:
    Jan 28, 2020
    Posts:
    66
    Thanks for your answer ! I've seen your post, it's the only result I found on the internet.

    So I will try to make a force array as an output, and loop it in the main thread to apply the force on the rigidbody. The main overhead is by computing the waves which definitely need some multi-threading.
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Yeah it'd be nice if there were more examples out there using GameObjects and components. Something in a dynamic changing environment.
    All examples I found of the C# job system use a pre-filled non-changing array rather than adding and removing elements.
    Especially in my case with projectiles they get active and inactive a lot.
     
  5. The_Storm_Rider

    The_Storm_Rider

    Joined:
    Jan 28, 2020
    Posts:
    66
    I thought C# jobs was more mainstream, it seems that we have to invent solutions on our own.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Jobs have been around for many years, well before the ECS and other DOTS tech but jobs work with value types i.e. copies of data which is what in itself makes it thread-safe. The fact that components and other objects are reference types and a lot of the Unity API is not threadsafe doesn't change how jobs work.

    Any system has limitations. You're getting super safe and pretty efficient multi-threading yet you're under the impression you're having to "roll your own solutions". Yes, it has a limitation but you get a lot for that price, especially when you add the Burst compiler into the picture. You can do a heck of a lot of heavy lifting with it.

    If you want to go full MT physics then yes, you need to use the DOTS specific physics because that's what it's designed for.