Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Instantiating a sword prefab on an enemy

Discussion in 'Entity Component System' started by jhuntley7, Jun 29, 2023.

  1. jhuntley7

    jhuntley7

    Joined:
    Jun 6, 2023
    Posts:
    2
    Hello,

    I'm hoping that someone might be able to point me in the right direction. I'm trying to spawn a sword next to an enemy. I've done lots of testing and I can see that an entity is being assigned to the sword prefab component that I've baked into enemy, but no sword appears. While testing, I did notice that the scale for the prefab was set to zero, but even changing that to a ridiculously high number produced no results.

    Here is the code I have so far...it is extremely janky. I've twisted everything around trying to get this to work and I'm just returning to programming after being away for nearly a decade!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using Unity.Entities;
    5. using Unity.Burst;
    6. using UnityEngine;
    7. using TMPro;
    8. using Unity.Transforms;
    9. using Unity.Mathematics;
    10. using Unity.Collections;
    11.  
    12. namespace Moonworld
    13. {
    14.     public partial struct EnemyAttackSystem : ISystem
    15.     {
    16.  
    17.         public Entity _playerEntity;
    18.  
    19.  
    20.         void OnCreate(ref SystemState state)
    21.         {
    22.             state.RequireForUpdate<CombatComponent>();
    23.         }
    24.  
    25.         void OnUpdate(ref SystemState state)
    26.         {
    27.  
    28.             // Get query and then array to access enemies in the foreach
    29.             var query = state.EntityManager.CreateEntityQuery(typeof(Enemy));
    30.             var enemyEntities = query.ToEntityArray(Allocator.TempJob);
    31.  
    32.             int iTracker = 0;
    33.  
    34.             var ecbSingletonForEach = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
    35.             EntityCommandBuffer ecbForEach = ecbSingletonForEach.CreateCommandBuffer(state.WorldUnmanaged);
    36.  
    37.             foreach (var (enemy, sword) in SystemAPI.Query<RefRO<Enemy>, RefRW<SwordComponent>>())
    38.             {
    39.  
    40.                 if (enemy.ValueRO.currentState == 1)
    41.  
    42.                 {
    43.                     if (enemy.ValueRO.isSwordOut == false)
    44.                     {                    
    45.                         Entity swordEntity = state.EntityManager.Instantiate(sword.ValueRW.swordPrefab);
    46.                         ecbForEach.AddComponent(swordEntity, new Parent { Value = enemyEntities[iTracker] });
    47.                         Debug.Log(swordEntity + " /// " + enemyEntities[iTracker].ToString());
    48.                     }
    49.                 }
    50.  
    51.                 if (enemy.ValueRO.currentState == 0)
    52.                 {
    53.                     if (enemy.ValueRO.isSwordOut == true)
    54.                     {
    55.                         // DESPAWN SWORD
    56.                         /*Entity swordEntity = state.EntityManager.Instantiate(sword.ValueRW.swordPrefab);
    57.                         Debug.Log("Sword entity spawned");*/
    58.                     }
    59.                 }
    60.  
    61.                 iTracker++;
    62.  
    63.             }
    64.  
    65.             var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
    66.  
    67.             new EnemySwordCheckJob { ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged) }.Schedule();
    68.  
    69.         }
    70.  
    71.  
    72.  
    73.  
    74.     }
    75.  
    76. }
    And here is the job referenced at the end of the last piece of code.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System;
    4. using Unity.Entities;
    5. using Unity.Burst;
    6. using UnityEngine;
    7. using TMPro;
    8. using Unity.Transforms;
    9. using Unity.Mathematics;
    10.  
    11.  
    12. namespace Moonworld
    13. {
    14.     [BurstCompile]
    15.     [WithAll(typeof(Enemy))]
    16.     public partial struct EnemySwordCheckJob : IJobEntity
    17.     {
    18.         public EntityCommandBuffer ecb;
    19.  
    20.         // Check enemy's current state and then update whether its sword should be out or not
    21.         [BurstCompile]
    22.         private void Execute(ref Enemy enemy, ref SwordComponent sword, ref LocalTransform transform)
    23.         {
    24.  
    25.             //Debug.Log(enemy.ValueRO.currentState);
    26.             if (enemy.currentState == 1)
    27.             {
    28.                 if (enemy.isSwordOut == false)
    29.                 {
    30.                     enemy.isSwordOut = true;
    31.                     Debug.Log("Sword entity transform being set...");
    32.                     ecb.SetComponent(sword.swordPrefab, new LocalTransform {
    33.                         Position = new float3 (transform.Position.x + 1, transform.Position.y + 1, transform.Position.z) });
    34.                 }
    35.             }
    36.  
    37.             if (enemy.currentState == 0)
    38.             {
    39.                 if (enemy.isSwordOut == true)
    40.                 {
    41.                     enemy.isSwordOut = false;
    42.                 }
    43.             }
    44.  
    45.         }
    46.     }
    47.  
    48. }
    Any help is appreciated! I feel like there is a far more elegant solution to this, but I've found it very challenging to access the variables I need in both systems and in jobs. I can also provide code for the baker if that would help.

    Thank you!
     
  2. jhuntley7

    jhuntley7

    Joined:
    Jun 6, 2023
    Posts:
    2
    Figured it out :D
     
  3. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Good for you ! but how ?
    It's quite frustrating for someone looking to fix an issue and finding a post saying I have this issue, then saying fixed it but 'ot explaining neither what was the cause of the issue 'or the way it was fixed.
    Not saying I have the issue, just reminding that someone might like to know the answer in the future.
     
    imaginadio and RBogdy like this.