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

Update Entity Friction

Discussion in 'Physics for ECS' started by iiziziz, Mar 24, 2020.

  1. iiziziz

    iiziziz

    Joined:
    Mar 20, 2014
    Posts:
    11
    Hi there,

    I'm trying to modify my player friction in a Job, (by now an
    IJobChunk
    )

    I finally found that I needed to access the
    Material
    component to access
    Friction
    but I didn't found a way to pass it to my job and to execute any logic on it.

    Did I miss something ?
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    This thread is talking about toggling collision events but the sample code can be easily extended to change any element of the
    Material
    struct including
    Friction
    .
    What issues are you having? What does your Job look like at the moment?
     
  3. iiziziz

    iiziziz

    Joined:
    Mar 20, 2014
    Posts:
    11
    By now, my code looks like that

    Code (CSharp):
    1. private struct PlayerDashSystemJob : IJobChunk
    2. {
    3.     [ReadOnly]
    4.     public ArchetypeChunkComponentType<DirectionComponent> DirectionComponentType;
    5.     [ReadOnly]
    6.     public ArchetypeChunkComponentType<InputComponent> InputComponentType;
    7.  
    8.     public ArchetypeChunkComponentType<DashComponent> DashComponentType;
    9.     public ArchetypeChunkComponentType<PhysicsVelocity> PhysicsVelocityComponentType;
    10.          
    11.     // TODO : Add no friction to material
    12.  
    13.     public void Execute(
    14.         ArchetypeChunk chunk,
    15.         int chunkIndex,
    16.         int firstEntityIndex)
    17.     {
    18.         var dashComponents = chunk.GetNativeArray(DashComponentType);
    19.         var physicsVelocities = chunk.GetNativeArray(PhysicsVelocityComponentType);
    20.         var inputComponents = chunk.GetNativeArray(InputComponentType);
    21.  
    22.         for (var i = 0; i < chunk.Count; i++)
    23.         {
    24.             if (inputComponents[i].Dash && dashComponents[i].CanDash)
    25.             {
    26.                 var dashComponent = dashComponents[i];
    27.                 var physicsVelocity = physicsVelocities[i];
    28.  
    29.                 physicsVelocity.Linear.x = dashComponent.Force;
    30.  
    31.                 dashComponent.CanDash = false;
    32.                 dashComponent.IsDashing = true;
    33.                 dashComponent.LastDashTick = DateTime.UtcNow.Ticks;
    34.  
    35.                 dashComponents[i] = dashComponent;
    36.                 physicsVelocities[i] = physicsVelocity;
    37.             }
    38.         }
    39.     }
    40. }
    I don't understand how to access Material data in my job as they are not ComponentData.
     
  4. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    The Material is accessed via the
    PhysicsCollider
    component, so you'll need to add those
    var physicsColliders = chunk.GetNativeArray(PhysicsColliderComponentType);

    Then use the code inside the spoiler block in the thread I linked above.
     
  5. iiziziz

    iiziziz

    Joined:
    Mar 20, 2014
    Posts:
    11
    Thanks, i'll check that