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

Access two transforms in a Job

Discussion in 'Entity Component System' started by PNordlund, May 1, 2018.

  1. PNordlund

    PNordlund

    Joined:
    Nov 20, 2013
    Posts:
    48
    I have a very simple job that is trying to keep objects facing the camera:

    struct KeepFacingCameraJob : IJobParallelFor
    {
    [ReadOnly]
    public TransformAccessArray cameras;
    [ReadOnly]
    public TransformAccessArray objects;

    public void Execute(int i)
    {
    objects = cameras.rotation;
    }
    }

    This produces an error about unsafe pointers. I could use IJobParallelForTransform but then I can only access a single transform. How can I access multiple transforms in a job?
     
  2. LazyGameDevZA

    LazyGameDevZA

    Joined:
    Nov 10, 2016
    Posts:
    143
    From what I know the `TransformAccessArray` is part of the hybrid functionality that's been added. In this case it'll provide access to the `Transform` object, NOT a struct type that can be safely accessed from a job. Also note that you've marked the `TransformAccessArray` you're transforming as `ReadOnly` which is an optimization attribute used to determine if a job can be run concurrently with another because it's only accessing data and not changing it, thus not able to cause race conditions.
     
  3. PNordlund

    PNordlund

    Joined:
    Nov 20, 2013
    Posts:
    48
    Thanks. I can work around this by using the IJobParallelForTransform to modify the target object's transform and storing the camera rotations into an array I can index in the Job instead of accessing the camera's transform itself. It's a bit clumsy of course.

    But I guess the question is, how can I make a job that can use one transform to modify another transform. Practical example - I have a bunch of child objects which are dependant on their parent's position/rotation/movement in some non-linear way.
     
  4. LazyGameDevZA

    LazyGameDevZA

    Joined:
    Nov 10, 2016
    Posts:
    143
    I've looked through some of the `Entities` code and came across the `TransformParentComponent` you could use to parent the Transforms to the camera objects. The built-in `TransformSystem` takes care of matching rotation to that of the parent if the components are matched correctly.
     
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I have that exact case (almost), child objects that need to rotate based on where the parent is. I just use separate jobs for each where the child job depends on the parent job.