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

Question Passing TransformAspect

Discussion in 'Entity Component System' started by T3M4CH, Jan 27, 2023.

  1. T3M4CH

    T3M4CH

    Joined:
    Aug 29, 2019
    Posts:
    5
    I've got an error when trying pass transformAspect from "Zombie Entity"
    My idea was create bullets and shoot them in Zombies by passing transformAspect

    Code Error :
    Code (CSharp):
    1. InvalidOperationException: The ComponentTypeHandle<Unity.Collections.NativeText.ReadOnly> has been declared as [WriteOnly] in the job, but you are reading from it.Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <9157942190d54845a13242589f74e64d>:0)
    2. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <9157942190d54845a13242589f74e64d>:0)
    3. Unity.Entities.RefRW`1[T].get_ValueRO () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.15/Unity.Entities/Iterators/RefRW.cs:135)
    4. Unity.Transforms.TransformAspect.get_WorldPosition () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.15/Unity.Transforms/TransformAspect.cs:72)
    5. Core.Scripts.ZombiesExample.ComponentsAndTags.MachineGun.BulletAspect.Move (System.Single deltaTime) (at Assets/Core/Scripts/ZombiesExample/ComponentsAndTags/MachineGun/BulletAspect.cs:17)
    6. Core.Scripts.ZombiesExample.Systems.Firearm.BulletJob.Execute (Core.Scripts.ZombiesExample.ComponentsAndTags.MachineGun.BulletAspect bulletAspect) (at Assets/Core/Scripts/ZombiesExample/Systems/Firearm/FlyBulletSystem.cs:42)
    It's begin in BulletAspect
    Code (CSharp):
    1.  public readonly partial struct BulletAspect : IAspect
    2.     {
    3.         public readonly Entity Entity;
    4.  
    5.         private readonly TransformAspect _transformAspect;
    6.         private readonly RefRO<BulletProperties> _bulletProperties;
    7.         private readonly RefRW<BulletTargetProperties> _bulletTargetProperties;
    8.  
    9.         public void Move(float deltaTime)
    10.         {
    11.             var direction = math.normalize(ZombieTransform.LocalPosition - _transformAspect.LocalPosition);
    12.             _transformAspect.LocalPosition += direction * _bulletProperties.ValueRO.Speed * deltaTime;
    13.         }
    14.  
    15.         private TransformAspect ZombieTransform => _bulletTargetProperties.ValueRO.ZombieWalkAspect.Transform;
    16.     }
    The way I passing it

    Code (CSharp):
    1. [BurstCompile]
    2.     public partial struct SpawnBulletSystem : ISystem
    3.     {
    4.         ...
    5.         [BurstCompile]
    6.         public void OnUpdate(ref SystemState state)
    7.         {
    8.             var deltaTime = SystemAPI.Time.DeltaTime;
    9.  
    10.             var ecb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>()
    11.                 .CreateCommandBuffer(state.WorldUnmanaged);
    12.  
    13.             var zombiesList = new List<ZombieWalkAspect>();
    14.  
    15.             foreach (var walkAspect in SystemAPI.Query<ZombieWalkAspect>().WithNone<TargetTag>())
    16.             {
    17.                 zombiesList.Add(walkAspect);
    18.             }
    19.  
    20.             if (zombiesList.Count <= 0) return;
    21.  
    22.             var zombieWalkAspect = zombiesList.First();
    23.  
    24.             new SpawnJob()
    25.             {
    26.                 DeltaTime = deltaTime,
    27.                 ZombieWalkAspect = zombieWalkAspect,
    28.                 EntityCommandBuffer = ecb,
    29.             }.Schedule();
    30.         }
    31.     }
    32.  
    33.     [BurstCompile]
    34.     public partial struct SpawnJob : IJobEntity
    35.     {
    36.         public float DeltaTime;
    37.         [NativeDisableUnsafePtrRestriction] public ZombieWalkAspect ZombieWalkAspect;
    38.         public EntityCommandBuffer EntityCommandBuffer;
    39.  
    40.         [BurstCompile]
    41.         private void Execute(FirearmAspect firearmAspect)
    42.         {
    43.             firearmAspect.EstimatedTime -= DeltaTime;
    44.  
    45.             if (!firearmAspect.CheckTimer()) return;
    46.  
    47.             var bulletEntity = EntityCommandBuffer.Instantiate(firearmAspect.BulletPrefab);
    48.            
    49.             EntityCommandBuffer.AddComponent(bulletEntity, new BulletTargetProperties
    50.             {
    51.                 ZombieWalkAspect = ZombieWalkAspect
    52.             });
    53.            
    54.             EntityCommandBuffer.AddComponent(ZombieWalkAspect.Entity, new TargetTag());
    55.         }
    56.     }
    I was try to change _bulletTargetProperties to RW and set RW in other getter

    Also I want to show you my Transform getter in ZombieWalkAspect

    Code (CSharp):
    1.  public readonly partial struct ZombieWalkAspect : IAspect
    2.     {
    3.         public readonly Entity Entity;
    4.  
    5.         private readonly TransformAspect _transform;
    6.         private readonly RefRW<ZombieTimer> _walkTimer;
    7.         private readonly RefRO<ZombieWalkProperties> _walkProperties;
    8.         private readonly RefRO<ZombieHeading> _heading;
    9.  
    10.         public void Walk(float deltaTime)
    11.         {
    12.             WalkTimer += deltaTime;
    13.             _transform.LocalPosition += _transform.Forward * WalkSpeed * deltaTime;
    14.  
    15.             var swayAngle = WalkAmplitude * math.sin(WalkFrequency * WalkTimer);
    16.             _transform.LocalRotation = quaternion.Euler(0, Heading, swayAngle);
    17.         }
    18.  
    19.         public bool IsInStoppingRange(float3 brainPosition, float brainRadiusSq)
    20.         {
    21.             return math.distancesq(brainPosition, _transform.LocalPosition) <= brainRadiusSq;
    22.            
    23.         }
    24.  
    25.         private float WalkTimer
    26.         {
    27.             get => _walkTimer.ValueRO.Value;
    28.             set => _walkTimer.ValueRW.Value = value;
    29.         }
    30.  
    31.         public TransformAspect Transform => _transform;
    32. }
    I'll be glad if someone help me figure it out