Search Unity

How do i access an entities transform inside a Job?

Discussion in 'Entity Component System' started by Vyvernn, Mar 27, 2021.

  1. Vyvernn

    Vyvernn

    Joined:
    Mar 3, 2020
    Posts:
    7
    I have the following code, basically i am trying to reuse entities, when a cloud goes out of range of the player i am moving it back to the other side of the player to emulate a rolling sky. But i cant directly affect an objects transform, i get an error saying the Transform is a managed IComponent and can only be used with .WithoutBurst and .Run but this job is using .Run so it has me confused

    Code (CSharp):
    1.  
    2. Entities.ForEach((Entity entity, ref Transform t, ref ConstantMovement constantMovement, in CloudComponent cloudComponent) =>
    3.         {
    4.             //Update the direction and moveSpeed of the clouds
    5.             constantMovement.Direction = direction;
    6.             constantMovement.moveSpeed = moveSpeed;
    7.  
    8.             //Check whether this cloud has gone out of the spawnrange
    9.             if (Vector3.Distance(t.position, playerPos) > threshold)
    10.             {
    11.                 //Check that the current number of clouds doesnt exceed the maximum
    12.                 if(numOfClouds<= maxCloudCount)
    13.                 {
    14.                     //Create Random
    15.                     System.Random r = new System.Random();
    16.  
    17.                     //Generate vector in the opposite direction of cloud movement on edge of spawn
    18.                     Vector3 offset = (direction.normalized*-1) * threshold;
    19.  
    20.                     //Apply a random offset along the x&z axis
    21.                     offset.x += (direction.normalized.x * Mathf.Lerp(-threshold, threshold, (float)r.NextDouble()));
    22.                     offset.z += (direction.normalized.z * Mathf.Lerp(-threshold, threshold, (float)r.NextDouble()));
    23.                    
    24.                     //Reset Height
    25.                     offset.y = playerPos.y + cloudHeight + Mathf.Lerp(-cloudHeightRange, cloudHeightRange, (float)r.NextDouble());
    26.  
    27.                     //Set transform position to the generated vector
    28.                     t.position = playerPos + offset;
    29.  
    30.                 }
    31.                 //If it does then destroy this cloud instead of repopulating it
    32.                 else
    33.                 {
    34.                     //Destroy cloud
    35.                 }
    36.             }
    37.  
    38.         }).Run();
    39.  
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    By default everything with .Run() is compiled with burst. Unless you specify .WithoutBurst() that is.

    Note that there's a TransformAccess that allows to read / write from transforms in an actual job.
    (.Run -> runs on a main thread without jobs)
     
    Vyvernn likes this.
  3. Resshin27

    Resshin27

    Joined:
    Apr 21, 2018
    Posts:
    31
    In ECS World, in your case, you need to access your Entity's Translation component and update it as per your game logic.
    Avoid using Transforms in ECS.
    Surely first check Unity Transforms page in Entity docs for clarity on how Transform component conversion of Gameobjects into ECS world happens.

    And,
    When using Jobs in Monobehaviour world, you can go for a IJobParallelForTransform & TransformAccessArray for updating Transform data directly.
     
    Vyvernn likes this.
  4. Vyvernn

    Vyvernn

    Joined:
    Mar 3, 2020
    Posts:
    7
    How do i directly access it's translation? using "Translation" doesn't work as it did when i set the entities original position
     
  5. Vyvernn

    Vyvernn

    Joined:
    Mar 3, 2020
    Posts:
    7
    Never mind, i just realised i didnt have include Transforms in my file, i assumed i had since "Transform" was working. Thanks!